title: Regex matching unescaped commas in Java
body: <p><strong>Problem description</strong></p>
  
  <p>I am trying to split a into separate strings, with the split() method that the String class provides. The documentation tells me that it will split around matches of the argument, which is a regular expression. The delimiter that I use is a comma, but commas can also be escaped. Escaping character that I use is a forward slash / (just to make things easier by not using a backslash, because that requires additional escaping in string literals in both Java and the regular expressions).</p>
  
  <p>For instance, the input might be this:</p>
  
  <pre><code>a,b/,b//,c///,//,d///,
  </code></pre>
  
  <p>And the output should be:</p>
  
  <pre><code>a
  b,b/
  c/,/
  d/,
  </code></pre>
  
  <p>So, the string should be split at each comma, unless that comma is preceded by an odd number of slashes (1, 3, 5, 7, ..., ∞) because that would mean that the comma is escaped.</p>
  
  <p><strong>Possible solutions</strong></p>
  
  <p>My initial guess would be to split it like this:</p>
  
  <pre><code>String[] strings = longString.split("(?&lt;![^/](//)*/),");
  </code></pre>
  
  <p>but that is not allowed because Java doesn't allow infinite look-behind groups. I could limit the recurrence to, say, 2000 by replacing the * with {0,2000}:</p>
  
  <pre><code>String[] strings = longString.split("(?&lt;![^/](//){0,2000}/),");
  </code></pre>
  
  <p>but that still puts constraints on the input. So I decided to take the recurrence out of the look-behind group, and came up with this:</p>
  
  <pre><code>String[] strings = longString.split("(?&lt;!/)(?:(//)*),");
  </code></pre>
  
  <p>However, its output is the following list of strings:</p>
  
  <pre><code>a
  b,b (the final slash is lacking in the output)
  c/, (the final slash is lacking in the output)
  d/,
  </code></pre>
  
  <p>Why are those slashes omitted in the 2nd and 3rd string, and how can I solve it (in Java)?</p>