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.

Advertisements

One Response to Regex to split a String on comma outside double quotes.

  1. SRK says:

    Great ! It works !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: