Java Regex - 拆分逗号分隔列表,但在括号内排除逗号

时间:2013-02-09 23:43:48

标签: java regex comma brackets

我正在尝试编写将这样拆分Java字符串的正则表达式:

300x250,468x60,300x400v(480x320,768x1024,100x100),400x300v,640x480v(200x200,728x90)

进入类似的事情:

300x250 
468x60
300x400v(480x320,768x1024,100x100)
400x300v
640x480v(200x200,728x90)

我一直在尝试\,(\()?,但最终也会在括号中选择逗号。

任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:5)

如果必须使用正则表达式,则可以在,(?![^(]*\\))

上拆分

如果没有那么一个简单的迭代超过字符可以做到这一点

String data="300x250,468x60,300x400v(480x320,768x1024,100x100),400x300v,640x480v(200x200,728x90)";

List<String> tokens=new ArrayList<>();
StringBuilder buffer=new StringBuilder();

int parenthesesCounter=0;

for (char c : data.toCharArray()){
    if (c=='(') parenthesesCounter++;
    if (c==')') parenthesesCounter--;
    if (c==',' && parenthesesCounter==0){
        //lets add token inside buffer to our tokens
        tokens.add(buffer.toString());
        //now we need to clear buffer  
        buffer.delete(0, buffer.length());
    }
    else 
        buffer.append(c);
}
//lets not forget about part after last comma
tokens.add(buffer.toString());

String[] splitedArray=tokens.toArray(new String[tokens.size()]);

//lets test what is inside our array
for (String s : splitedArray)
    System.out.println(s);

输出

300x250
468x60
300x400v(480x320,768x1024,100x100)
400x300v
640x480v(200x200,728x90)

答案 1 :(得分:0)

akburg,恢复这个问题以完成,因为它有另一个未提及的简单解决方案。这种情况类似于Match (or replace) a pattern except in situations s1, s2, s3 etc

这是我们简单的正则表达式:

\([^)]*\)|(,)

交替的左侧匹配完整的(parentheses)标签。我们将忽略这些匹配。右侧匹配并捕获第1组的逗号,我们知道它们是正确的逗号,因为它们与左侧的表达式不匹配。

此程序显示了如何使用正则表达式(请参阅online demo底部的结果):

import java.util.*;
import java.io.*;
import java.util.regex.*;
import java.util.List;

class Program {
public static void main (String[] args) throws java.lang.Exception  {

String subject = "300x250,468x60,300x400v(480x320,768x1024,100x100),400x300v,640x480v(200x200,728x90)";
Pattern regex = Pattern.compile("\\([^)]*\\)|(,)");
Matcher m = regex.matcher(subject);
StringBuffer b= new StringBuffer();
while (m.find()) {
if(m.group(1) != null) m.appendReplacement(b, "SplitHere");
else m.appendReplacement(b, m.group(0));
}
m.appendTail(b);
String replaced = b.toString();
String[] splits = replaced.split("SplitHere");
for (String split : splits) System.out.println(split);
} // end main
} // end Program

参考

How to match (or replace) a pattern except in situations s1, s2, s3...