2014-03-12 55 views
0

正則表達式我有一個字符串象下面這樣:如何捕捉在Java

"This is the code: cd001, cd002, cd003 "

(我已經搭上:CD001,CD002,CD003)

,但它必須忽略:CD001,CD002 ,CD003下面

"This is the code: cd001,cd002, cd003,xxxx "

在字符串中我有一個正則表達式:[^|\\s|>]*([a-z]{2}[0-9]+\\.?)\\b

(以起始字符串開頭,空格然後是兩個小寫字母,數字後面,然後是[。或#或空格])

+0

您能否重新格式化您的問題,並明確說明輸入和預期匹配? – sp00m

+0

我已經解決了我的問題,希望它更好! –

+2

爲什麼要忽略第二個輸入?規則是什麼?你的兩個輸入是完全一樣的...... – sp00m

回答

0
// parse inputString into String[] of codes 
// if there are no codes in the string, codes[0] is "" 
String[] codes = 
// delete beginning of the line till ":" inclusive 
    inputString.replaceFirst("^.*: ", ""). 
// delete two codes that are separated by "," and 
// followed by 0 or 1 "," and 1 " " 
    replaceAll("[a-z0-9]+,[a-z0-9]+,? ", ""). 
// delete trailing spaces 
    replaceFirst(" +$", ""). 
// split codes 
    split(", ");