Regex find comma not inside quotes

Stand back and be amazed!


Here is the regex you seek:

(?!\B"[^"]*),(?![^"]*"\B)


Here is a demonstration:

regex101 demo


  • It does not match the second line because the " you inserted does not have a closing quotation mark.
  • It will not match values like so: ,r"a string",10 because the letter on the edge of the " will create a word boundary, rather than a non-word boundary.

Alternative version

(".*?,.*?"|.*?(?:,|$))

This will match the content and the commas and is compatible with values that are full of punctuation marks

regex101 demo


The below regex is for parsing each fields in a line, not an entire line

Apply the methodical and desperate regex technique: Divide and conquer

Case: field does not contain a quote

  • abc,
  • abc(end of line)

[^,"]*(,|$)

Case: field contains exactly two quotes

  • abc"abc,"abc,
  • abc"abc,"abc(end of line)

[^,"]*"[^"]*"[^,"]*(,|$)

Case: field contains exactly one quote

  • abc"abc(end of line)
  • abc"abc, (and that there's no quote before the end of this line)

[^,"]*"[^,"]$

[^,"]*"[^"],(?!.*")

Now that we have all the cases, we then '|' everything together and enjoy the resultant monstrosity.

Tags:

Regex