title: Java - string.split with colliding regexes
body: <p>I recently came across this problem and don't know how to solve it. I know that in the String class, we have the <a href="https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)" rel="nofollow noreferrer">split</a> method which accepts a regex and based on the regex, the given string is split into different strings and returned in a string array.</p>
  
  <p>For example, if I have,</p>
  
  <pre><code>String s = "A,B,C";
  </code></pre>
  
  <p>and I do,</p>
  
  <pre><code>System.out.println(Arrays.toString(s.split(",")));
  </code></pre>
  
  <p>it will print [A, B, C] to output console.</p>
  
  <p>Now let's say my string is </p>
  
  <pre><code>String s = "A,\"\"B\"\",\"\"C\"\",D";   //easier to read version: A,""B"",""C"",D
  </code></pre>
  
  <p>and I use the following regex to split the string,</p>
  
  <pre><code>String regex = ",|,\"\"|\"\",|\"\",\"\"";  // matches , OR ,"" OR "", OR "",""
  System.out.println(Arrays.toString(s.split(regex)));
  </code></pre>
  
  <p>I get the output as [A, ""B, ""C, D]. How is the splitting working over here? And how do I define my regex so that I get [A, B, C, D] as my output? </p>
  
  <p>NOTE: I know what I want to achieve can be done in other ways (like replaceAll method), but I only want to use the String.split for this problem, since I want to know how to use it in this case.</p>