简体   繁体   中英

How can I write a regular expression that will not match if a string contains a particular 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 want something a little bit like the like the non-matching character class (eg [^abc]), but for whole strings:
.*?(^NOOOT).*?

Does such a creature exist?

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

Explanation:

^ start of string

(?!NOOOT). assert that it's not possible to match NOOOT at the current position, then match any character.

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

$ end of string.

You can do that wit a negative lookahead assertion (?!…) :

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

This one matches only if there is no NOOOT ahead at the current position while proceeding character by character.

It's also possible to do that with basic syntax. But that's more complex.

Complementing regexes is a bad idea. It's possible but really bad style, and may be inefficient; and the negative assertions that allow you to do it concisely may only hide the inefficiency. Almost always the better idea is to match the regexp normally and invert the result with ! .

You should only ever compose negative regexes if you absolutely have to, eg if you must satisfy an API that can only be programmed by supplying one regext and nothing else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM