Negative lookahead with capturing groups

2019-06-19 14:25发布

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.

1条回答
唯我独甜
2楼-- · 2019-06-19 14:33
^(?!.*(.)(.)\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

查看更多
登录 后发表回答