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, quote, carriage return, or newline | abc123 |
| |
Alternation (OR operator) | N/A |
" |
Literal double quote character | " |
( |
Start of a non-capturing group (for alternation) | N/A |
[^"] |
Character class matching any character except a double quote | a |
| |
Alternation (OR operator) | N/A |
"" |
Escaped double quote (represents a single quote in the string) | "" |
) |
End of non-capturing group | N/A |
* |
Quantifier: zero or more repetitions of the preceding group | "a""b" |
" |
Literal double quote character | " |
) |
End of capturing group (Group 1) | N/A |
( |
Start of a non-capturing group (repeated for additional fields) | N/A |
, |
Literal comma character | , |
( |
Start of a capturing group (Group 4) | N/A |
[^,"\r\n]+ |
Character class matching one or more characters that are not comma, quote, carriage return, or newline | xyz789 |
| |
Alternation (OR operator) | N/A |
" |
Literal double quote character | " |
( |
Start of a non-capturing group (for alternation) | N/A |
[^"] |
Character class matching any character except a double quote | b |
| |
Alternation (OR operator) | N/A |
"" |
Escaped double quote (represents a single quote in the string) | "" |
) |
End of non-capturing group | N/A |
* |
Quantifier: zero or more repetitions of the preceding group | "b""c" |
" |
Literal double quote character | " |
) |
End of capturing group (Group 4) | N/A |
) |
End of non-capturing group (repeated for additional fields) | N/A |
* |
Quantifier: zero or more repetitions of the preceding group (allows zero or more fields after the first) | ,field2,field3 |
$ |
Anchor: asserts position at end of the string | End of string |
Matches
field1,field2,field3field1,"field,with,commas",field3"field with ""quotes""",simplea,b,c""123,456,789a,"b""c",d
Does not match
field1,field2 field3field1,"unterminated,field2field1,,field3field1,"field with newline",field3field1,"field"extrafield1,"field""field1,"field"""
Common pitfalls
- Unterminated quoted fields (missing closing quote) will not match.
- Newlines within fields (even in quoted fields) will cause failure.
- Empty fields between commas (e.g., 'a,,b') are not matched by this pattern.
- Trailing commas (e.g., 'a,b,') are not matched.
- Escaped quotes must be doubled (e.g., '""' for a single quote in a quoted field).
- The pattern does not allow spaces around commas (e.g., 'a, b' fails).
Performance notes
This pattern has a risk of catastrophic backtracking due to nested quantifiers (especially the * inside the quoted field pattern). For very long strings, consider a more efficient parser. The ^ and $ anchors ensure the entire string is validated, which is good for correctness but may impact performance on large inputs.