How to exclude exact strings (not substrings) from matches in a regular expression?

I have found many questions here on how to exclude a substring from the results, but I want to exclude strings that are exact matches, and simply cannot figure out how to do this.

With the test data below, how would I compare everything except 11 and 111 ?

 0 1 00 01 10 11 000 001 010 011 100 101 110 111 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 

I tried different things, for example:

 ^((?!11|111).)*$ 

But this eliminates substring matches when again I want to exclude exact matches.

Is this possible with regex? If so, how can exact matches be eliminated?

+5
source share
2 answers

You need to have the end of the line included in the negative forecast:

 ^(?!(11|111)$).*$ 

Watch the demo (using your data)

Without including the end of the line, you say that the input does not start with 11 or 111 , when you want to say that all input (from start to end) isn’t 11 or 111 .

+6
source

Through the verb PCRE (*SKIP)(*F) ,

 ^(?:11|111)$(*SKIP)(*F)|.+ 

Demo

OR

 ^(?:(?!^(?:111|11)$).)++$ 

Demo

+1
source

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


All Articles