Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / regular-expression

A small CSV Splitter in C#

1.00/5 (1 vote)
7 Oct 2010CPOL 14.7K  
Gives splitted values taking csv string as input.
C#
public string[] SplitCSVString(string csvString)
        {
            // CSV Reg Ex: ",(?!(?<=(?:^|,)\s*\x22(?:[^\x22]|\x22\x22|\\x22)*,)(?:[^\x22]|\x22\x22|\\x22)*\x22\s*(?:,|$))"
            string strPattern = ",(?!(?<=(?:^|,)\\s*\\x22(?:[^\\x22]|\\x22\\x22|\\\\x22)*,)(?:[^\\x22]|\\x22\\x22|\\\\x22)*\\x22\\s*(?:,|$))";
            System.Text.RegularExpressions.Regex csvSplitter = new System.Text.RegularExpressions.Regex(strPattern);
            return csvSplitter.Split(csvString);
        }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)