Regular Expressions 101

Community Patterns

Replace Commas Inside Quotes

1

Regular Expression
PCRE (PHP <7.3)

/
["](""[^"]*""|[^",]*)[,]?(""[^"]*""|[^",]*)[,]?(""[^"]*""|[^",]*)[,]?(""[^"]*""|[^",]*)[,]?(""[^"]*""|[^",]*)[,]?(""[^"]*""|[^",]*)[,]?(""[^"]*""|[^",]*)[,]?(""[^"]*""|[^",]*)[,]?(""[^"]*""|[^",]*)[,]?["]
/
g

Description

Regex to remove commas within quotes. (Common situtaion in .csv files)

Eg: data,"some ""embedded"" stuff,commas,inside,quotes",more

Replace String would be something like this: \1&\2&\3&\4&\5&\6&\7&\8&\9

which would replace the commas inside the quotes with '&'s Caveats:

  • embedded quotes ("") remain in the matches
  • limit of 9 terms within quotes

Work in progress ...

Submitted by Judd - 10 years ago