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) | Start of field pattern |
[^",\r\n]+ |
Character class matching one or more characters that are not double quotes, carriage returns, or line feeds | abc123 |
| |
Alternation: OR operator between two patterns | Separates quoted and unquoted field patterns |
" |
Literal double quote character | Start of quoted field |
([^"]|""|" )* |
Group 2: Matches zero or more occurrences of either a non-quote character, two double quotes (escaped quote), or a space followed by a quote (edge case for quoted spaces) | field content with "escaped" or " quoted |
" |
Literal double quote character | End of quoted field |
) |
End of capturing group (Group 1) | End of field pattern |
( |
Start of a non-capturing group (outer quantifier group) | Start of repetition for additional fields |
, |
Literal comma character | Field separator |
( |
Start of capturing group (Group 3) for the next field | Start of next field pattern |
[^",\r\n]+ |
Character class matching one or more characters that are not double quotes, carriage returns, or line feeds | nextfield |
| |
Alternation: OR operator between two patterns | Separates quoted and unquoted field patterns |
" |
Literal double quote character | Start of quoted next field |
([^"]|""|" )* |
Group 4: Matches zero or more occurrences of either a non-quote character, two double quotes (escaped quote), or a space followed by a quote | next field content with "escaped" |
" |
Literal double quote character | End of quoted next field |
) |
End of capturing group (Group 3) | End of next field pattern |
) |
End of non-capturing group (outer quantifier group) | End of repetition for additional fields |
* |
Quantifier: zero or more occurrences of the preceding group | Allows zero or more additional fields |
$ |
Anchor: asserts position at end of the string | End of string |
Matches
abc,def,ghi"quoted field",unquoted"field with ""escaped"" quotes",simple" space at end" ,valuea,"b c",d
Does not match
abc,,defabc,def,abc,"def"abc"def"abc,"def "abc,"def "abc,"def"ghi"
Common pitfalls
- Fails to handle fields containing literal backslashes (not part of the pattern's design)
- Does not support multi-line quoted fields (only single-line fields with \r\n excluded)
- The pattern for quoted spaces (" ) is unusual and may not match intended cases
- No validation for balanced quotes within quoted fields
- Does not handle empty fields between commas (e.g., a,,b) correctly as it requires at least one character in unquoted fields
Performance notes
The pattern has potential for catastrophic backtracking due to nested quantifiers and alternations, especially with long quoted fields containing many escaped quotes. The use of * quantifiers without upper bounds exacerbates this risk. Anchoring at both start (^) and end ($) helps limit the scope but does not eliminate the risk entirely. For large inputs, consider a more constrained pattern or a parser.