Exclude "abba" pattern strings

For example, I want to exclude fitting, hollow, trillion
but not "hello" or "pattern"

I already have a job to work

(.)(.)\2\1 

which matches “hollow” or “fitting”, but I cannot deny it.

the closest thing i get is

 ^.(?!(.)(.)\2\1) 

which excludes “fit” and “hollow,” but not “trillion”

+6
source share
2 answers

This is a little different from what you have. The current current regular expression will check for pallindromicity (?) From the second character. Since you want to check the entire line, you need to change it a bit:

 ^(?!.*(.)(.)\2\1) 

The first anchor ensures that the check is performed only at the beginning (otherwise, the regular expression may require a match at the end of the line).

Then .* Within the negative forecast will allow you to check anywhere on the line. If there is a match, the entire match cannot be completed.

+3
source

This is not trillion because you added ^. means that he must have a symbol before the start of the match. For your first two cases, it has the character h and f . Therefore, if you change this to ^..(?!(.)(.)\2\1) , then it will work for trillion .

So, in the general case, the regex will be:

 (?!.*(.)(.)\2\1) ^^ any number of characters(other than \n) 
+2
source

All Articles