regex - Negative lookahead with capturing groups -
i'm attempting challenge:
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
.
Comments
Post a Comment