[Solved] Regex pattern for split


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"]

2

solved Regex pattern for split