如何在java中捕获正则表达式

时间:2014-03-12 10:50:49

标签: java regex

我有一个如下字符串:

"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

(以开始字符串开头,空格然后是两个小写字母,后面是数字,然后是[。或,或#或空格])

1 个答案:

答案 0 :(得分: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(", ");