Match "," or new-line, but not when inside quotations

41 views
Skip to first unread message

Gustav Stensson

unread,
Jan 20, 2016, 2:24:54 PM1/20/16
to Regex
Hi!

I need some help with a regex. I would like to match each comma(,), newline(\n) or carriage return(\r), but not when any of these are in between two quotation marks(")
for example the following string should have the highlighted matches:

Hello, my, name is, "something, you never", knew
that, was a line break, "btw
this is not matched"

so this would be 7 matches, including the line break, except the one wrapped with ".

this is the most complex I got /(?!("(.*,.*)*")),|\r|\n/g however that matches all of them (i thought the ?! would exclude stuff...)

I'm not very advanced with regex, which is why I'm here. I've tried back and forth on regex101.com, with no progress.

Any help is appreciated!

thanks!

iiz

unread,
Jan 21, 2016, 4:17:43 AM1/21/16
to Regex
Hi,

What do you want to do with your matches? You could use the principle of 'good string bad string'. Basically you have an alternating group. Bad stuff on the left of the pipe, good stuff on the right. By using capture groups you capture the good stuff. No I dont't know which language you are using, but some allow you to refer to capturing groups when matching.

So this would be it:
\".*?\"|([,\r\n])
\1 would refer to the stuff you need to match.



Regards,

Theo.




Op woensdag 20 januari 2016 20:24:54 UTC+1 schreef Gustav Stensson:
Reply all
Reply to author
Forward
0 new messages