Regular Expressions 101

Community Patterns

1...45678...757

parsing CSV file

5

Regular Expression
PCRE (PHP <7.3)

/
(?:(?<=^)|(?<=,)) # start of line or comma (?:(?!$)\s)* # skip whitespace "? # skip opening quote mark ( (?<=").*?(?=") # capture value in quotes | # or ((?!,)\S)* # capture just value ) "? # skip closing quote mark (?:(?!$)\s)* # skip whitespace (?=$|,) # command or the end of the line
/
gmx

Description

It correctly handles simple values, empty values, and quoted values with commas inside.

Submitted by Viktor Lova aka nsinreal - 9 years ago