Negative lookahead with capturing groups
Asked Answered
B

1

15

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.

Beatabeaten answered 30/9, 2015 at 9:16 Comment(0)
S
12
^(?!.*(.)(.)\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

Sokotra answered 30/9, 2015 at 9:20 Comment(1)
I see, that makes perfect sense. Thanks for the explanation. I've also never come across the Regex101 site before, that will be useful if I ever have to post examples again.Beatabeaten

© 2022 - 2024 — McMap. All rights reserved.