Search code examples
regexregex-negationregex-lookaroundsbackreferencecapturing-group

Negative lookahead with capturing groups


I'm attempting this challenge:

https://regex.alf.nu/4

I want to match all strings that don't contain an ABBA pattern.

Match:

aesthophysiology
amphimictical
baruria
calomorphic

Don't Match

anallagmatic
bassarisk
chorioallantois
coccomyces
abba

Firstly, I have a regex to determine the ABBA pattern.

(\w)(\w)\2\1

Next I want to match strings that don't contain that pattern:

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

However this matches everything.

If I simplify this by specifying a literal for the negative lookahead:

^((?!agm).)*$

The the regex does not match the string "anallagmatic", which is the desired behaviour.

So it looks like the issue is with me using capturing groups and back-references within the negative lookahead.


Solution

  • ^(?!.*(.)(.)\2\1).+$
    
        ^^
    

    You can use a lookahead here.See demo.The lookahead you created was correct but you need add .* so that it cannot appear anywhere in the string.

    https://regex101.com/r/vV1wW6/39

    Your approach will also work if you make the first group non capturing.

    ^(?:(?!(\w)(\w)\2\1).)*$
    
     ^^
    

    See demo.It was not working because \2 \1 were different than what you intended.In your regex they should have been \3 and \2.

    https://regex101.com/r/vV1wW6/40