我无法根据正则表达式拆分字符串。
String str = "1=(1-2,3-4),2=2,3=3,4=4";
Pattern commaPattern = Pattern.compile("\\([0-9-]+,[0-9-]+\\)|(,)") ;
String[] arr = commaPattern.split(str);
for (String s : arr)
{
System.out.println(s);
}
预期输出,
1=(1-2,3-4)
2=2
3=3
4=4
实际输出,
1=
2=2
3=3
4=4
最佳答案
此正则表达式将根据需要拆分
,(?![^()]*\\))
------------
|->split with , only if it is not within ()
关于java - 如何使用正则表达式根据模式拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15699353/