Regex to match specific strings without a given prefix

Regex

Regex Problem Overview


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

Regex Solutions


Solution 1 - Regex

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. (?<!)).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGuillaumeView Question on Stackoverflow
Solution 1 - RegexHowardView Answer on Stackoverflow