regexnegative-lookbehind

How to make the regex negative lookbehind consume discarded text?


My regex is: /(?<!start )".*?"/ - it searches for quotes in text (actually for string literals in code)

For me, it works perfectly with these cases:

  1. start "text" - doesn't match
  2. 123 "text" - match
  3. "text" - match

But with cases like this it doesn't:

  1. start "test " start " text" - it will match when I don't want it to (with " start ")

How can I make the regex negative lookbehind consume discarded text?


Solution

  • You can use SKIP FAIL. There are other ways too, if you elaborate:

    start\s+"[^"\r\n]*"(*SKIP)(*F)|"[^"\r\n]*"
    

    Also see


    Or:

    \bstart\s+"[^"\r\n]*"(*SKIP)(*F)|"[^"\r\n]*"
    

    See


    Details