regex

Regex to match specific strings without a given prefix


I need to match all of its lines that contains a value and that don't have a given prefix.

Example: I want all lines that contains word when it's not prefixed by prefix

So:

foobar -> no match
prefix word -> no match
prefix word suffix -> no match
word -> MATCH
something word -> MATCH

What I've tried so far:

(?!prefix)word

Doesn't seem to do what I want


Solution

  • You may need

    (?<!prefix )word
    

    (and maybe take care of the spaces).

    (?!) is a negative lookahead but in your case you need a negative lookbehind (i.e. (?<!)).

    Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Lookbehind_assertion