Close this search box.

RegEx for CSV

After much searching, this is the best RegEx I can find for splitting a line of text from a CSV file:

I found it here:

Here is the magical working code:

protected virtual string[] SplitCSV(string line) {
  System.Text.RegularExpressions.RegexOptions options =
      ((System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace |
        System.Text.RegularExpressions.RegexOptions.Multiline) |
  Regex reg =
      new Regex("(?:^|,)(\\\"(?:[^\\\"]+|\\\"\\\")*\\\"|[^,]*)", options);
  MatchCollection coll = reg.Matches(line);
  string[] items = new string[coll.Count];
  int i = 0;
  foreach (Match m in coll) {
    items[i++] = m.Groups[0].Value.Trim('"').Trim(',').Trim('"').Trim();
  return items;

posted on Saturday, September 04, 2004 8:15 AM

This article is part of the GWB Archives. Original Author: mwatson

Related Posts