title: C#, regular expressions : how to parse comma-separated values, where some values might be quoted strings themselves containing commas
body: <p>In C#, using the <code>Regex</code> class, how does one parse comma-separated values, where some values might be quoted strings themselves containing commas?</p>
  
  <pre><code>using System ;
  using System.Text.RegularExpressions ;
  
  class  Example
      {
      public static void Main ( )
          {
          string  myString  =  "cat,dog,\"0 = OFF, 1 = ON\",lion,tiger,'R = red, G = green, B = blue',bear" ;
          Console.WriteLine ( "\nmyString is ...\n\t" + myString + "\n" ) ;
          Regex   regex  =  new Regex  (  "(?&lt;=,(\"|\')).*?(?=(\"|\'),)|(^.*?(?=,))|((?&lt;=,).*?(?=,))|((?&lt;=,).*?$)"  )  ;
          Match   match  =  regex.Match ( myString ) ;
          int j = 0 ;
          while ( match.Success )
              {
              Console.WriteLine ( j++ + " \t" + match ) ;
              match  =  match.NextMatch() ;
              }
          }
      }
  </code></pre>
  
  <p>Output (in part) appears as follows:</p>
  
  <pre><code>0       cat
  1       dog
  2       "0 = OFF
  3        1 = ON"
  4       lion
  5       tiger
  6       'R = red
  7        G = green
  8        B = blue'
  9       bear
  </code></pre>
  
  <p>However, <em>desired</em> output is:</p>
  
  <pre><code>0       cat
  1       dog
  2       0 = OFF, 1 = ON
  3       lion
  4       tiger
  5       R = red, G = green, B = blue
  6       bear
  </code></pre>