I need to match all strings that contain one word of a list, but only if that word is not immediately preceded by another specific word. I have this regex:
.*(?<!forbidden)\b(word1|word2|word3)\b.*
that is still matching a sentence like hello forbidden word1
because forbidden
is matched by .*
. But if I remove the .*
I am not anymore matching strings like hello word1
, which I want to match.
Note that I want to match a string like forbidden hello word1
.
Could you suggest me how to fix this problem?