Search code examples
regexcsvquote

Regular expression for csv with commas and no quotes


I'm trying to parse really complicated csv, which is generated wittout any quotes for columns with commas.
The only tip I get, that commas with whitespace before or after are included in field.

Jake,HomePC,Microsoft VS2010, Microsoft Office 2010

Should be parsed to

Jake
HomePC
Microsoft VS2010, Microsoft Office 2010

Can anybody advice please on how to include "\s," and ,"\s" to column body.


Solution

  • If your language supports lookbehind assertions, split on

    (?<!\s),(?!\s)
    

    In C#:

    string[] splitArray = Regex.Split(subjectString, 
        @"(?<!\s) # Assert that the previous character isn't whitespace
        ,         # Match a comma
        (?!\s)    # Assert that the following character isn't whitespace", 
        RegexOptions.IgnorePatternWhitespace);