Regex to split a String on comma outside double quotes.

Suppose you have a string like so:

String str = "abc,foo,c;bar=\"this,demo\",hello\"world,syzygy\"";

and you want to write regex to split this string on comma. Well, no big deal in this. But you would notice that there are some comma inside the double quotes too. Now what if you want to ignore those commas while splitting?

Here’s working code that will do it:

String str = "abc,foo,c;bar=\"this,demo\",hello\"world,syzygy\"";
String[] tokens = str.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
		
for (String token: tokens) {
    System.out.println(token);
}

This would give you the following output:

abc
foo
c;bar="this,demo"
hello"world,syzygy"

So, what that regex is doing? The trick is to split on commas that are followed by an even number of double quotes (Of course this would work only if the quotes are balanced). Enjoy.