I would like to resolve this problem.
,
comma : split terms \"
double quote : String value (ignore special char)This regex does the trick:
",(?=(([^\"]*\"){2})*[^\"]*$)(?=([^\\[]*?\\[[^\\]]*\\][^\\[\\]]*?)*$)"
It works by adding a look-ahead for matching pairs of square brackets after the comma - if you're inside a square-bracketed term, of course you won't have balanced brackets following.
Here's some test code:
String line = "a=1,b=\"1,2,3\",c=[d=1,e=\"1,11\"]";
String[] tokens = line.split(",(?=(([^\"]*\"){2})*[^\"]*$)(?=([^\\[]*?\\[[^\\]]*\\][^\\[\\]]*?)*$)");
for (String t : tokens)
System.out.println(t);
Output:
a=1
b="1,2,3"
c=[d=1,e="1,11"]
I know the question is nearly a year old, but... this regex is much simpler:
\[[^]]*\]|"[^"]*"|(,)
|
matches [complete brackets]
|
matches \"strings like this\"
Splitting on Group 1 Captures
You can do it like this (see the output at the bottom of the online demo):
String subject = "a=1,b=\"1,2,3\",c=[d=1,e=\"1,11\"]";
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);
This is a two-step split: first, we replace the commas with something distinctive, such as @@SplitHere@@
Pros and Cons
{inside , curlies}
, you just add another OR
branch to the left of the regex: {[^{}]*}
Reference
This technique has many applications. It is fully explained in these two links.