String line = "a=1,b=\"1,2\",c=\"[d=1,e=1,11]\"";
String[] tokens = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)|,(?=\"[\\([^]]*\\)|[^\"]]*\")");
for (String t : tokens) {
System.out.println("> " + t);
}
System.out.println("-----------------------");
Console
> a=1
> b="1,2"
> c=[d=1
> e="1,1"]
-----------------------
I want to result
Console
> a=1
> b="1,2"
> c=[d=1,e="1,1"]
-----------------------
Help for java regex pattern to split comma(,)
Thanks
I would program it:
output:
You can use this code:
This regex matches a comma ONLY if it is followed by even number of double quotes. Thus commas inside double quote aren't matched however however all outside commas are used for splitting your input.
PS: This will work for balanced quoted strings only .e.g. this won't work:
"a=1,b=\"1,2"
as double quote is unbalanced.OUTPUT:
Try this one
,(?=\\w=(\".+\"))
I tried your sample code in netbeans, I got this out put.
Isnt this what you want?