Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts position at start of the string | Start of 'abc,def' (matches) |
( |
Start of a capturing group (group 1) | ... |
[^",\r\n]+ |
Character class: one or more characters that are not double quotes, carriage returns, or newlines | abc, 123, or xYz |
| |
Alternation: OR operator between two patterns | ... |
" |
Literal double quote character | Start of quoted field like "abc" |
([^"]|"")* |
Group: zero or more occurrences of either a non-quote character or a pair of double quotes (escaped quote) | abc, a""bc, or "" |
" |
Literal double quote character | End of quoted field like "abc" |
) |
End of capturing group (group 1) | ... |
( |
Start of a non-capturing group (for repetition) | ... |
, |
Literal comma character | Separator between fields like in 'abc,def' |
( |
Start of a capturing group (group 2, but unused) | ... |
[^",\r\n]+ |
Character class: one or more characters that are not double quotes, carriage returns, or newlines | abc, 123, or xYz |
| |
Alternation: OR operator | ... |
" |
Literal double quote character | Start of quoted field like "abc" |
([^"]|"")* |
Group: zero or more occurrences of either a non-quote character or a pair of double quotes (escaped quote) | abc, a""bc, or "" |
" |
Literal double quote character | End of quoted field like "abc" |
) |
End of capturing group (group 2, but unused) | ... |
) |
End of non-capturing group | ... |
* |
Quantifier: zero or more occurrences of the preceding group | Matches 'abc,def' or 'abc' |
$ |
Anchor: asserts position at end of the string | End of 'abc,def' (matches) |
Matches
abc,def,ghi123,"a""b",456"quoted field",unquotedsimple""a,"b,c",dfield1,field2,field31,"2,3",4
Does not match
abc,"def"ghi",jkl,abc,"def""abc,defabc,"def"ghi,abcabc,,def"abc"def"
Common pitfalls
- Fails to handle escaped quotes outside of quoted fields (e.g., abc"def).
- Does not account for spaces around commas (e.g., 'abc, def').
- May incorrectly match strings with trailing commas if not anchored properly.
- Does not validate CSV rules like balanced quotes in quoted fields.
- Fails on fields containing literal carriage returns or newlines.
Performance notes
The pattern has nested quantifiers (especially the * inside the quoted field pattern) which can lead to catastrophic backtracking for malformed input (e.g., long strings of quotes). Always validate input length or use possessive quantifiers if possible. The ^ and $ anchors ensure full-string matching, which is correct for CSV validation.