Search code examples
regexregex-lookarounds

Regular expressions, negative lookahead anywhere in the string


Im sorry if this is asked and has an answer but I can't find it.

I know about regex lookarounds and negative lookahead.

Thing is that negative lookahead examines what comes right after current position in a string.

What I need is to find and discard matches if string contains words like "career(s)" and "specials" for example, but if it contains them anywhere in the string.

What would be the efficient way of doing that?

At the moment I'm using PCRE flavor but the more general regex is, the better.

Thank you.


Solution

  • You can use this regex:

    ^(?!.*(?:career\(s\)|specials)).*
    

    Or if s is optional then use:

    ^(?!.*(?:career|special)s?).*
    

    RegEx Demo