Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try:</p> <pre><code>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("&gt; "+t); } } } </code></pre> <p>Output:</p> <pre><code>&gt; foo &gt; bar &gt; c;qual="baz,blurb" &gt; d;junk="quux,syzygy" </code></pre> <p>In other words: <em>split on the comma only if that comma has zero, or an even number of quotes ahead of it</em>.</p> <p>Or, a bit friendlier for the eyes:</p> <pre><code>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("&gt; "+t); } } } </code></pre> <p>which produces the same as the first example.</p> <h2>EDIT</h2> <p>As mentioned by @MikeFHay in the comments:</p> <blockquote> <p>I prefer using <a href="http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Splitter.html" rel="noreferrer">Guava's Splitter</a>, as it has saner defaults (see discussion above about empty matches being trimmed by <code>String#split()</code>, so I did: </p> <pre><code>Splitter.on(Pattern.compile(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)")) </code></pre> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload