Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts position at start of the string | Start of string |
( |
Start of a capturing group (Group 1) | N/A |
[^,"\r\n]+ |
Character class matching one or more characters that are not comma, double-quote, carriage return, or line feed | abc, 123, field1 |
| |
Alternation (OR operator) | N/A |
, |
Matches a literal comma | , |
| |
Alternation (OR operator) | N/A |
" |
Start of a double-quoted string | " |
([^"]|"")* |
Non-capturing group matching zero or more occurrences of either: (a) any character except double-quote, or (b) two consecutive double-quotes (escaped quote) | abc", """ (escaped quote) |
" |
End of a double-quoted string | " |
) |
End of capturing group (Group 1) | N/A |
( |
Start of a non-capturing group (repeated) | N/A |
, |
Matches a literal comma | , |
( |
Start of a capturing group (Group 3) | N/A |
[^,"\r\n]+ |
Character class matching one or more characters that are not comma, double-quote, carriage return, or line feed | abc, 123, field1 |
| |
Alternation (OR operator) | N/A |
, |
Matches a literal comma | , |
| |
Alternation (OR operator) | N/A |
" |
Start of a double-quoted string | " |
([^"]|"")* |
Non-capturing group matching zero or more occurrences of either: (a) any character except double-quote, or (b) two consecutive double-quotes (escaped quote) | abc", """ (escaped quote) |
" |
End of a double-quoted string | " |
) |
End of capturing group (Group 3) | N/A |
) |
End of non-capturing group (repeated) | N/A |
* |
Quantifier: zero or more occurrences of the preceding group | N/A |
$ |
Anchor: asserts position at end of the string | End of string |
Matches
abc,123,field1a,"b,c",d"""quoted""",unquotedfield1,field2,field3a,"",bx,y,z1,2,3
Does not match
abc,123,field1,a,"b,c"dfield1,field2 field3a,"b"c,dabc,"defa,,ba,"b"c
Common pitfalls
- Fails to handle empty fields correctly if not quoted (e.g., 'a,,b' is invalid).
- Does not account for escaped quotes outside quoted fields (e.g., 'a,"b"c,d' is invalid).
- May incorrectly match strings with unescaped newlines within quoted fields.
- Does not validate CSV structure beyond field delimiters (e.g., missing trailing comma).
Performance notes
The pattern is prone to catastrophic backtracking if the input is malformed (e.g., many consecutive commas or unmatched quotes). Always validate input length and structure before applying. Anchors (^ and $) help limit the scope but do not prevent backtracking in the middle of the pattern.