java regex pattern split commna
java, regex
Solution
You can use this code:
String line = "a=1,b=\"1,2\",c=\"[d=1,e=1,11]\"";
String[] tokens = line.split(",(?=(([^\"]*\"){2})*[^\"]*$)");
for (String t : tokens)
System.out.println("> " + t);
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:
> a=1
> b="1,2"
> c="[d=1,e=1,11]"
Problem
``` 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