Parse CSV row using Regex
CodeKeep C# Feed Maggio 22nd, 2008
Description: Returns an array of values from a CSV row handling columns in double and single quotes. This has been adapted from the example code found here : http://www.omegacoder.com/?p=60
Link: http://www.codekeep.net/snippets/5d115467-5515-451a-800e-14623f49fc9d.aspx
string[] ParseCSVLine(string text)
{
Regex rx = new Regex("((?([\\x27\\x22])(?:[\\x27\\x22])(?<Column>[^\\x27\\x22]*)(?:[\\x27\\x22])|(?<Column>[^,\r\n]*]*))(?:,?))+(?:$|[\r\n]{0,2})", RegexOptions.IgnorePatternWhitespace);
List<string> line = new List<string>();
foreach (Match m in rx.Matches(text))
{
if (m.Success)
{
foreach (Capture cp in m.Groups["Column"].Captures)
{
if (string.IsNullOrEmpty(cp.Value) == false)
line.Add(cp.Value);
}
}
}
return line.ToArray();
}






