11

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

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

4 回答 4

6

对于非嵌套

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

FOR NESTED(括号内的括号)

(?<!\([^\)]*),(?![^\(]*\))
于 2012-10-06T04:26:22.473 回答
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);
}
于 2012-10-06T04:15:43.713 回答
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
于 2012-10-06T05:14:12.717 回答
0

试试这个

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

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

  • \w{3}(?=,)- 匹配 3 个任意字母数字字符(包括下划线),并积极向前看逗号

  • (?<=,)\(\w{3},\w{3}\)(?=,)- 匹配这个模式(ABC,E4R),也做一个积极的前瞻,并在后面寻找逗号

  • (?<=,)\w{3} - matches the 3 any alphanumeric character (including underscore) and does the positive look behind for comma

于 2012-10-06T05:44:43.503 回答