Skip to content

Instantly share code, notes, and snippets.

@philipmat
Created February 28, 2014 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philipmat/9273379 to your computer and use it in GitHub Desktop.
Save philipmat/9273379 to your computer and use it in GitHub Desktop.
Regex for CSV
var d = @"Col1,Col2,Col3,Col4
Foo,100,""Foo,Bar"",""Foo
Bar
Baz""
Bar,101,,Quux";
var rx = new Regex("((?<=^|,)(?<field>)(?=,|$)|(?<field>[^\",\\r\\n]+)|\"(?<field>([^\"]|\"\")*)\")(,|(?<rowbreak>\\r\\n|\\n|$))", RegexOptions.Multiline);
var matches = rx.Matches(d);
matches[6].Groups["field"].Dump();
matches.Dump();
var d2 = "Foo\r\nBar\rBaz\nQuux";
d2.Split(new[] { '\r', '\n' }, 2).Dump();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment