Created
February 28, 2014 15:48
-
-
Save philipmat/9273379 to your computer and use it in GitHub Desktop.
Regex for CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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