regex - Negative lookahead with capturing groups -


i'm attempting challenge:

https://regex.alf.nu/4

i want match strings don't contain abba pattern.

match:

aesthophysiology amphimictical baruria calomorphic 

don't match

anallagmatic bassarisk chorioallantois coccomyces abba 

firstly, have regex determine abba pattern.

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

next want match strings don't contain pattern:

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

however matches everything.

if simplify specifying literal negative lookahead:

^((?!agm).)*$ 

the regex not match string "anallagmatic", desired behaviour.

so looks issue me using capturing groups , back-references within negative lookahead.

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

you can use lookahead here.see demo.the lookahead created correct need add .* cannot appear anywhere in string.

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

your approach work if make first group non capturing.

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

see demo.it not working because \2 \1 different intended.in regex should have been \3 , \2.

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


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -