Friday, March 20, 2009

How to split csv file with regular expression

This function is similar to the string.Split function, except it works with a line from csv file and it using c# Regex class.

protected string[] Split(string s)
{
Regex r = new Regex(",(?=([^']*'[^']*')*(?![^']*'))");
List list = new List();

int sStart = 0;
int nCount = 0;

foreach (Match m in r.Matches(s))
{
list.Add(s.Substring(sStart, m.Index - sStart).Trim('"'));
sStart = m.Index + 1;
}
list.Add(s.Substring(sStart, s.Length - sStart).Trim('"'));

return list.ToArray();
}

No comments: