Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: splitting a comma-separated string but ignoring commas in quotes

Tags:

java

string

regex

I have a string vaguely like this:

foo,bar,c;qual="baz,blurb",d;junk="quux,syzygy" 

that I want to split by commas -- but I need to ignore commas in quotes. How can I do this? Seems like a regexp approach fails; I suppose I can manually scan and enter a different mode when I see a quote, but it would be nice to use preexisting libraries. (edit: I guess I meant libraries that are already part of the JDK or already part of a commonly-used libraries like Apache Commons.)

the above string should split into:

foo bar c;qual="baz,blurb" d;junk="quux,syzygy" 

note: this is NOT a CSV file, it's a single string contained in a file with a larger overall structure

like image 292
Jason S Avatar asked Nov 18 '09 16:11

Jason S


People also ask

How do you split comma separated string in Java?

To split a string with comma, use the split() method in Java. str. split("[,]", 0);

How do you split a string with double quotes?

Use method String. split() It returns an array of String, splitted by the character you specified.

How do you parse a comma separated string?

In order to parse a comma-delimited String, you can just provide a "," as a delimiter and it will return an array of String containing individual values. The split() function internally uses Java's regular expression API (java. util. regex) to do its job.


1 Answers

Try:

public class Main {      public static void main(String[] args) {         String line = "foo,bar,c;qual=\"baz,blurb\",d;junk=\"quux,syzygy\"";         String[] tokens = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);         for(String t : tokens) {             System.out.println("> "+t);         }     } } 

Output:

> foo > bar > c;qual="baz,blurb" > d;junk="quux,syzygy" 

In other words: split on the comma only if that comma has zero, or an even number of quotes ahead of it.

Or, a bit friendlier for the eyes:

public class Main {      public static void main(String[] args) {         String line = "foo,bar,c;qual=\"baz,blurb\",d;junk=\"quux,syzygy\"";          String otherThanQuote = " [^\"] ";         String quotedString = String.format(" \" %s* \" ", otherThanQuote);         String regex = String.format("(?x) "+ // enable comments, ignore white spaces                 ",                         "+ // match a comma                 "(?=                       "+ // start positive look ahead                 "  (?:                     "+ //   start non-capturing group 1                 "    %s*                   "+ //     match 'otherThanQuote' zero or more times                 "    %s                    "+ //     match 'quotedString'                 "  )*                      "+ //   end group 1 and repeat it zero or more times                 "  %s*                     "+ //   match 'otherThanQuote'                 "  $                       "+ // match the end of the string                 ")                         ", // stop positive look ahead                 otherThanQuote, quotedString, otherThanQuote);          String[] tokens = line.split(regex, -1);         for(String t : tokens) {             System.out.println("> "+t);         }     } } 

which produces the same as the first example.

EDIT

As mentioned by @MikeFHay in the comments:

I prefer using Guava's Splitter, as it has saner defaults (see discussion above about empty matches being trimmed by String#split(), so I did:

Splitter.on(Pattern.compile(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)")) 
like image 153
Bart Kiers Avatar answered Sep 29 '22 18:09

Bart Kiers