用逗号分隔字符串,但不带内括号

时间:2018-09-19 16:48:38

标签: java regex

更新: 它不能与Regex to match only commas not in parentheses?重复,因为它匹配字符串中的逗号,但是我想按逗号分割字符串,而不是查找逗号

我的测试字符串

String myString = "x1, x2, x3,  x4, x1(x1, x2,x3,x4)";

我想要的输出

x1
x2
x3
x4
x1(x1, x2,x3,x4)

我刚刚读过 How do I split a string by commas except inside parenthesis, using a regular expression?的答案,但是当我尝试将其连接到Java时发生错误

我尝试:

String[] parts = myString.split("((?:[^,(]+|(\((?>[^()]+|\g<-1>)*\)))+)");

错误:字符串文字中的非法转义符

我该怎么办?

0 个答案:

没有答案