title: Parsing CSV input with a RegEx in java
body: <p>I know, now I have two problems.  But I'm having fun!</p>
  
  <p>I started with <a href="http://www.regexguru.com/2009/04/split-is-not-always-the-best-way-to-split-a-string/" rel="noreferrer">this advice</a> not to try and split, but instead to match on what is an acceptable field, and expanded from there to this expression.</p>
  
  <pre><code>final Pattern pattern = Pattern.compile("\"([^\"]*)\"|(?&lt;=,|^)([^,]*)(?=,|$)");
  </code></pre>
  
  <p>The expression looks like this without the annoying escaped quotes:</p>
  
  <pre><code>"([^"]*)"|(?&lt;=,|^)([^,]*)(?=,|$)
  </code></pre>
  
  <p>This is working well for me - either it matches on "two quotes and whatever is between them", or "something between the start of the line or a comma and the end of the line or a comma".  Iterating through the matches gets me all the fields, even if they are empty.  For instance,</p>
  
  <pre><code>the quick, "brown, fox jumps", over, "the",,"lazy dog"
  </code></pre>
  
  <p>breaks down into</p>
  
  <pre><code>the quick
  "brown, fox jumps"
  over
  "the"
  
  "lazy dog"
  </code></pre>
  
  <p>Great!  Now I want to drop the quotes, so I added the lookahead and lookbehind non-capturing groups like I was doing for the commas.</p>
  
  <pre><code>final Pattern pattern = Pattern.compile("(?&lt;=\")([^\"]*)(?=\")|(?&lt;=,|^)([^,]*)(?=,|$)");
  </code></pre>
  
  <p>again the expression is:</p>
  
  <pre><code>(?&lt;=")([^"]*)(?=")|(?&lt;=,|^)([^,]*)(?=,|$)
  </code></pre>
  
  <p>Instead of the desired result</p>
  
  <pre><code>the quick
  brown, fox jumps
  over
  the
  
  lazy dog
  </code></pre>
  
  <p>now I get this breakdown:</p>
  
  <pre><code>the quick
  "brown
   fox jumps"
  ,over,
  "the"
  ,,
  "lazy dog"
  </code></pre>
  
  <p>What am I missing?</p>