How can we regex words after // on same line but out of quotes. My current regex:
CodePudding user response:
If supported, you could make use of SKIP FAIL to first match what you want to avoid
"[^"]*"(*SKIP)(*F)|//\K.
"[^"]*"(*SKIP)(*F)
Match"..."
and avoid that match|
Or//\K
Match//
and reset the starting point of the reported match.
Match the rest of the line
Another option could be matching what you don't want, and capture what you want to keep using an alternation |
and a capture group (. )
"[^"]*"|//(. )
CodePudding user response:
Try this one:
(?<=\" )(?!.*\").*
I've used both "positive lookbehind assertion" (?<=\" )
to check it is after the "
and "negative lookahead assertion" (?!.*\")
to check there is no "
after the comment. If the part is after the quote and there is no quote after it, it means it is not surrounded by "
.
In case you want the first line also matched use:
(?!.*\")//.*