# capture all two\d that are not preceded by fo+ $_ = 'footwo1 bootwo2 ftwo3 fotwo4'; $not = qr/fo+/; $want = qr/two\d/; print "Got $1\n" while /\G(?:(?!$not?$want).|$not$want)*($want)/g; #### s/\G((?:(?!$not?$want).|$not$want)*)($want)/$1X/g; #### s/\G$want/X/ while /\G(?:(?!$not?$want).|$not$want)*(?=$want)/g; #### s/\G((?:(?!$not?$want).|$not$want)*)\K($want)/X/g; # yielded Eval-group not allowed at runtime, use re 'eval' in regex m/\G((?:(?!(?-xism:fo+)?(?-xism:two\d)).|(?-xism:fo+)(?-xism:two\d))*)(?{Regexp::Keep::KEEP})((?-xism:two\d))/ at try2.pl line 9.