вторник, 2 ноября 2010 г.

regex for csv in c# .net

After much searching, this is the best RegEx I can find for splitting a line of text from a CSV file:
(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)

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) 
            | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
         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;
      }

Комментариев нет: