Regular Expressions 101

Community Patterns

split a comma separated string ignoring quotes and brackets

0

Regular Expression
PCRE2 (PHP >=7.3)

/
(?:'[^']*'|"[^"]*"|(\((?:[^()]++|(?1))*\))|[^'",])+
/
gm

Description

Breaks a CSV into parts, but keeps those things in quotes (single OR double), or in curved bracket together.

preg_match_all('~(?:\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'|"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|(\((?:[^()]++|(?1))*\))|[^\'",])+~s', $text, $matches)

Submitted by anonymous - a year ago