Regular Expressions 101

Community Patterns

Find all commas not in quotes

0

Regular Expression
PCRE (PHP <7.3)

/
,(?=(?:[^"]*"[^"]*")*[^"]*$)
/
gm

Description

I like have a utility that I use to load records in a DB. I usually save of data as csv, but the utility uses a slightly different (in my opinion simpler) format. The default delimiter is a '|' character. So I need to convert all the commas in the csv to '|', but I don't want to convert commas that are in quotes (quotes are usually used in most csv exports if the value contains a comma). The regex used in a find/replace in most editors accomplishes that. Kudos to the second answer on this SO question for this regex:

Submitted by anonymous - 5 years ago