I would like to find a regex that will pick out all commas that fall outside quote sets.
For example:
\'foo\' => \'bar\',
\'foofoo\' => \'bar,
The below regexes would match all the comma's which are present outside the double quotes,
,(?=(?:[^"]*"[^"]*")*[^"]*$)
DEMO
OR(PCRE only)
"[^"]*"(*SKIP)(*F)|,
"[^"]*"
matches all the double quoted block. That is, in this buz,"bar,foo"
input, this regex would match "bar,foo"
only. Now the following (*SKIP)(*F)
makes the match to fail. Then it moves on to the pattern which was next to |
symbol and tries to match characters from the remaining string. That is, in our output ,
next to pattern |
will match only the comma which was just after to buz
. Note that this won't match the comma which was present inside double quotes, because we already make the double quoted part to skip.
DEMO
The below regex would match all the comma's which are present inside the double quotes,
,(?!(?:[^"]*"[^"]*")*[^"]*$)
DEMO