First, this question may have been asked before, but I'm not sure what phrase to search on.
I have a string:
Maaaa
I have a pattern:
aaa
I would like to match twice, giving me starting indices of 1 and 2. But of course I only get a single match (start index 1), because the regex engine gobbles up all 3 "a"s and can't use them again, leaving me with 1开发者_如何学运维 "a" which doesn't match.
How do I solve this?
Thanks!
You could use a lookahead assertion to find an a followed by 2 a's
a(?=aa)
The man perlre
manpage suggests:
my @a;
"Maaaa" =~ /aaa(?{push @a,$&})(*FAIL)/;
print join "\n",@a;
print "\n";
which yields
aaa
aaa
精彩评论