I have a string like this:
a b c a b \" a b \" b a \" a \"
How do I match every a that is not part of a string deli
Full-blown regex solution for regex lover, without caring about performance or code-readability.
This solution assumes that there is no escaping syntax (with escaping syntax, the a in "sbd\"a" is counted as inside the string).
Pseudocode:
processedString =
inputString.replaceAll("\\".*?\\"","") // Remove all quoted strings
.replaceFirst("\\".*", "") // Consider text after lonely quote as inside quote
Then you can match the text you want in the processedString. You can remove the 2nd replace if you consider text after the lone quote as outside quote.
EDIT
In Ruby, the regex in the code above would be
/\".*?\"/
used with gsub
and
/\".*/
used with sub
To address the replacement problem, I'm not sure whether this is possible, but it worths trying:
/(\"|a)/ with gsub, and supply function.", then increment counter, and return " as replacement (basically, no change). If match is a check whether the counter is even: if even supply your replacement string; otherwise, just supply whatever is matched.