Home > Net >  Regex words on same line after specific characters but out of quotes
Regex words on same line after specific characters but out of quotes

Time:11-25

How can we regex words after // on same line but out of quotes. My current regex: enter image description here

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

Regex demo

Another option could be matching what you don't want, and capture what you want to keep using an alternation | and a capture group (. )

"[^"]*"|//(. )

Regex demo

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 ".

DEMO

In case you want the first line also matched use:

(?!.*\")//.*

DEMO

  • Related