Skip to content

Instantly share code, notes, and snippets.

@jrwren
Created August 30, 2011 21:06
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 jrwren/1182046 to your computer and use it in GitHub Desktop.
Save jrwren/1182046 to your computer and use it in GitHub Desktop.
why does this regex result in two matches?
public virtual string[] _ParseCsvFormat(string value)
{
//var regexstr = @"(?:(?:""(?<m>[^""]*?,[^""]*?)""|(?<m>[^,]*)))(?:,|$)";
var regexstr = @"
(?:
(?:
""
(?<m>[^""]*?,[^""]*?)
""
|
(?<m>[^,]*)
)
)
(?:,|$)
";
var regex = new Regex(regexstr, RegexOptions.IgnorePatternWhitespace);
var matches = regex.Matches(value);
return matches.Cast<Match>()
.Select(m => m.Groups["m"].Value)
.ToArray();
}
_ParseCsvFormat("a"); //results in {"a",""} and I was hoping for {"a"}
I don't see from where the second match is coming.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment