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: http://thedotnet.com/howto/work213583.aspx
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;
posted on Saturday, September 04, 2004 8:15 AM