根据正则表达式拆分字符串

时间:2012-10-06 04:08:08

标签: java regex

我有一个字符串需要根据“,”(逗号)的出现进行拆分,但需要忽略一对括号中出现的任何字符串。 例如,B2B,(A2C,AMM),(BNC,1NF),(106,A01),AAA,AX3 应该分成

B2B,
(A2C,AMM),
(BNC,1NF),
(106,A01),
AAA,
AX3

4 个答案:

答案 0 :(得分:6)

FOR NON NESTED

,(?![^\(]*\))

FOR NESTED (括号内的括号)

(?<!\([^\)]*),(?![^\(]*\))

答案 1 :(得分:2)

尝试以下:

var str = 'B2B,(A2C,AMM),(BNC,1NF),(106,A01),AAA,AX3';
console.log(str.match(/\([^)]*\)|[A-Z\d]+/g));
// gives you ["B2B", "(A2C,AMM)", "(BNC,1NF)", "(106,A01)", "AAA", "AX3"]

Java版:

String str = "B2B,(A2C,AMM),(BNC,1NF),(106,A01),AAA,AX3";
Pattern p = Pattern.compile("\\([^)]*\\)|[A-Z\\d]+");
Matcher m = p.matcher(str);
List<String> matches = new ArrayList<String>();
while(m.find()){
    matches.add(m.group());
}

for (String val : matches) {
    System.out.println(val);
}

答案 2 :(得分:2)

一个简单的迭代可能比任何正则表达式都更好,特别是如果你的数据在括号内有圆括号。例如:

String data="Some,(data,(that),needs),to (be, splited) by, comma";
StringBuilder buffer=new StringBuilder();
int parenthesesCounter=0;
for (char c:data.toCharArray()){
    if (c=='(') parenthesesCounter++;
    if (c==')') parenthesesCounter--;
    if (c==',' && parenthesesCounter==0){
        //lets do something with this token inside buffer
        System.out.println(buffer);
        //now we need to clear buffer  
        buffer.delete(0, buffer.length());
    }
    else 
        buffer.append(c);
}
//lets not forget about part after last comma
System.out.println(buffer);

输出

Some
(data,(that),needs)
to (be, splited) by
 comma

答案 3 :(得分:0)

试试这个

\w{3}(?=,)|(?<=,)\(\w{3},\w{3}\)(?=,)|(?<=,)\w{3}

说明:有三个部分由OR (|)

分隔
  • \w{3}(?=,) - 匹配3个任何字母数字字符(包括下划线),并为逗号做好积极展望

  • (?<=,)\(\w{3},\w{3}\)(?=,) - 匹配此模式(ABC,E4R),也会做出积极的向前看,并留意逗号

  • (?<=,)\w{3} - 匹配3个任何字母数字字符(包括下划线),并为逗号做正面看法