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) | Group 1 begins |
[^,"\r\n]+ |
Character class: one or more characters that are not comma, double quote, carriage return, or newline | abc, 123, field |
| |
Alternation (OR operator) | Either this or the next pattern |
" |
Literal double quote (start of quoted field) | " |
([^"]|""|" )* |
Non-capturing group (Group 2) repeated zero or more times: matches either a non-quote character, an escaped quote (two quotes), or a space followed by a quote | text, "", " " |
) |
End of Group 2 | End of quoted field content |
" |
Literal double quote (end of quoted field) | " |
) |
End of Group 1 (first field) | End of first field |
( |
Start of a non-capturing group (repeated zero or more times for subsequent fields) | Start of repeated field group |
, |
Literal comma (field separator) | , |
" |
Literal double quote (start of quoted field in repetition) | " |
([^"]|""|" )* |
Non-capturing group (Group 3) repeated zero or more times: same as Group 2 | text, "", " " |
) |
End of Group 3 | End of quoted field content in repetition |
" |
Literal double quote (end of quoted field in repetition) | " |
| |
Alternation (OR operator) | Either this or the next pattern |
[^,"\r\n]+ |
Character class: one or more non-comma, non-quote, non-newline characters | field |
) |
End of the non-capturing group (repeated fields) | End of repeated field |
* |
Quantifier: zero or more occurrences of the preceding group | Zero or more fields after the first |
$ |
Anchor: asserts position at end of the string | End of string |
Matches
abc,def,ghi"a,b",c,"d""""field1,"field, with, commas",field3" " , "" , "a"a,"b""c",d
Does not match
abc,"def ghi"abc,"def"ghi"abc,def,,abc,defabc,,def"abcabc"a,b c,d
Common pitfalls
- Unquoted fields containing commas or newlines will not match
- Escaped quotes must be represented as two consecutive quotes (e.g., "" inside a quoted field), not a backslash-escaped quote
- The pattern does not validate CSV structure beyond field separation (e.g., it allows empty fields like ,,)
- Newlines within fields are not allowed even in quoted fields (due to \r\n exclusion)
- The pattern may fail to match if the string ends with a comma (e.g., abc,def,)
Performance notes
The pattern is prone to catastrophic backtracking if the input contains many consecutive commas or malformed quoted fields (e.g., "a"""""""""""""""""b"). Anchoring (^ and $) helps limit the scope of matching. For large inputs, consider a streaming CSV parser instead.