2

Given the following data, I'd like a Regex to pull out each comma-separated value. However, a double-quoted value may contain commas.

"SMITH, JOHN",1234567890,"12/20/2012,11:00",,DRSCONSULT,DR BOB - OFFICE VISIT - CONSULT,SLEEP CENTER,1234567890,,,"a, b"
"JONES, WILLIAM",1234567890,12/20/2012,12:45,,DRSCONSULT,DR BOB - OFFICE VISIT - CONSULT,SLEEP CENTER,,,,

Here's the expression that I have so far:

(?<=^|,)(?:(?:(?<=\")([^\"]*)(?=\"))|(?:(?<![\"])([^,\"]*)(?![\"])))(?=$|,)

Regular expression visualization

Debuggex Demo

The double-quoted values are not being matched. What am I doing wrong? (This Regex is passed into pre-existing code - I cannot rewrite the system.)

8
  • Running a complex RegEx on a large CSV file will be noticeably slower than other methods of string processing.
    – Eric J.
    May 7, 2014 at 20:08
  • 6
    Somebody has to come along and say it, so it might as well be me: "Why don't you just use an existing CSV parser?"
    – Jon B
    May 7, 2014 at 20:09
  • 1
    @JonB: He states that pre-existing code requires that a RegEx be passed in.
    – Eric J.
    May 7, 2014 at 20:11
  • @EricJ. how would the existing code know how he finds the data, csv parser or regex?
    – Bit
    May 7, 2014 at 20:14
  • 3
    @N4TKD: This Regex is passed into pre-existing code - I cannot rewrite the system. Not sure what else I can say, other than he was pretty clear about that constraint.
    – Eric J.
    May 7, 2014 at 20:20

3 Answers 3

4

How about:

(?<=^|,)(("[^"]*")|([^,]*))(?=$|,)

Regular expression visualization

Debuggex Demo

The first alternative is:

("[^"]*")

Match a " followed by anything that's not a " followed by a "

The second alternative is just:

([^,]*)

Match anything that isn't a ,

1
  • 2
    Thanks for a useful answer! However, the resulting values should strip off the double-quotes.
    – harley.333
    May 7, 2014 at 20:33
0

This pattern should work:

(\w+\,\s\w+|[\d\/]*\,\d+\:\d*|[\w\d\:\s\-]+)

example:

http://regex101.com/r/rI8nS1

When using the pattern in C# you might need to escape it llke:

Match match = Regex.Match(searchText, "(?m)(?x)(\\w+\\,\\s\\w+|[\\d\\/]*\\,\\d+\\:\\d*|[\\w\\d\\:\\s\\-]+)"); 
    if (match.Success) {...}
0

Here's the code which I use for coping with quote-aware CSVs

//regex to translate a CSV
readonly Regex csvParser = new Regex( "(?:^|,)(\\\"(?:[^\\\"]+|\\\"\\\")*\\\"|[^,]*)", RegexOptions.Compiled);

//given a row from the csv file, loop through returning an array of column values
private IEnumerable<string> ProcessCsvRow(string row)
{
    MatchCollection results = csvParser.Matches(row);
    foreach (Match match in results)
    {
        foreach (Capture capture in match.Captures)
        {
            yield return (capture.Value ?? string.Empty).TrimStart(",").Trim('"', ' ');
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.