How to write a regular expression that will not match if the string contains a specific substring?

Example:

Suppose in the following example I want to match strings that do not contain the word "NOOOT".
Example A: This shirt is NOOOT black.
Example B: This shirt is black.

I need something a bit like an inappropriate character class (like [^ abc]), but for whole lines:
.*?(^NOOOT).*?

Does such a creature exist?

+4
source share
3 answers
 ^(?:(?!NOOOT).)*$ 

Explanation:

^ start of line

(?!NOOOT). argue that it is impossible to combine NOOOT at the current position, and then match any character.

(?: ...)* do this any number of times until ...

$ end of line.

+4
source

You can do this with a negative lookahead statement (?!…) :

 ^(?:(?!NOOOT).)*$ 

This only matches if the current position does not have NOOOT forward while continuing character by character.

It can also be done using basic syntax. But it is more difficult.

+4
source

Complementing regular expressions is a bad idea. It is possible, but very poor style, and can be ineffective; and negative statements that allow you to do this briefly can only mask inefficiencies. Almost always the best idea is to match the regular expression normally and invert the result with ! .

You should always write negative regular expressions if you absolutely need to, for example. if you must satisfy an API that can only be programmed by supplying one regext and nothing else.

+2
source

Source: https://habr.com/ru/post/1304544/


All Articles