开发者

Using a substring twice in regex

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜