CSV fields with quotes
A regex exercise from Regexdrill. Solve it with a pattern box with live highlighting in your browser, checked against hidden test strings, and it is checked the moment you submit.
The task
Match every field of a comma-separated line. A field is either text wrapped in double quotes, which may contain commas (the quotes are part of the match), or a run of one or more characters other than commas and quotes. Empty fields are skipped. The whole match is compared.
What this exercise teaches
"[^"]*"|[^,"]+ tries the quoted field first, so a comma inside quotes stays inside the field. A greedy ".*" would run from the first quote on the line to the last one.
A hint if you get stuck
Put the quoted form first in the alternation and use a negated class inside the quotes, so the match stops at the first closing quote.
Try it now, free, no account needed
Every exercise on Regexdrill is free to try. Open it on the practice page, solve it, and get instant feedback with a hint. A free account adds the worked solution; Pro adds timed mode and the certificate.
Open this exerciseMore exercises in Extraction and cleanup
- Log lines: level and messageFree
- key=value pairsFree
- The third CSV fieldFree
- HashtagsFree
- Host names from URLsFree
- Trim whitespaceFree
- Collapse repeated spacesFree
- Reformat MM/DD/YYYY as YYYY-MM-DDFree
- Swap Last, First to First LastFree
- Strip HTML tagsFree
- Keep only the digitsFree
- Normalize phone numbersFree
- Link text inside <a> tagsFree
- Web server access logFree