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 group |
[^,"\r\n]+ |
Character class: one or more characters that are not commas, double quotes, carriage returns, or line feeds (unquoted field) | abc,123,field |
| |
Alternation: OR operator between two patterns | Separates unquoted and quoted field patterns |
" |
Literal double quote: starts a quoted field | Start of quoted field |
([^"]|""|" )* |
Group (group 2) matching zero or more occurrences of: non-quote characters, escaped quotes ("), or space followed by quote (for trailing space handling) | content, "escaped" quote, trailing space |
) |
End of capturing group (group 1) | End of group |
( |
Start of a non-capturing group (outer group for repetition) | Start of repetition group |
, |
Literal comma: separates CSV fields | Comma separator |
( |
Start of a capturing group (group 3) | Start of group |
[^,"\r\n]+ |
Character class: one or more characters that are not commas, double quotes, carriage returns, or line feeds (unquoted field) | abc,123,field |
| |
Alternation: OR operator | Separates unquoted and quoted field patterns |
" |
Literal double quote: starts a quoted field | Start of quoted field |
([^"]|""|" )* |
Group (group 4) matching zero or more occurrences of: non-quote characters, escaped quotes ("), or space followed by quote | content, "escaped" quote, trailing space |
) |
End of capturing group (group 3) | End of group |
) |
End of non-capturing group (outer group for repetition) | End of repetition group |
* |
Quantifier: zero or more occurrences of the preceding group | Allows multiple fields separated by commas |
$ |
Anchor: asserts position at end of the string | End of string |
Matches
abc,123,field"quoted field",unquoted, "field with ""quote"""a,"b c",d""" "field1,field2,field3
Does not match
abc,,defabc,"defabc,def,abc,"def"ghiabc,"def "abc,"def "abc,def,ghi,
Common pitfalls
- Unquoted fields containing commas or newlines will not match
- Escaped quotes must be exactly two double quotes ("") inside quoted fields
- Trailing spaces after quotes in quoted fields are not preserved unless explicitly included in the pattern
- The pattern does not validate CSV content beyond structure (e.g., no numeric checks)
- Anchors (^ and $) ensure the entire string matches; partial matches will fail
Performance notes
The pattern has nested quantifiers (*) which can lead to catastrophic backtracking for malformed input (e.g., long sequences of quotes or commas). For large inputs, consider a more efficient parser. The alternation and nested groups increase complexity, so performance degrades with input size.