This might not be as slick as the lookahead approaches described above, but in the spirit of TMTOWTDI here's a version that uses pos and the @- array, which contains the offset of the start of the last match (see perlvar for more info):
sub count_matches
{
my ( $pattern, $string ) = @_;
my $num_matches = 0;
while( $string =~ m/$pattern/gi )
{
pos( $string ) = $-[0] + 1;
$num_matches++
}
return $num_matches;
}
Update: For monks (like me) that didn't understand why a pattern consisting entirely of a zero-width lookahead assertion (m/(?=AA)/g, see the above responses) doesn't get stuck in an infinite loop, see perlre, "Repeated patterns matching zero-length substring". From that doc:
Perl allows such constructs, by forcefully breaking the infinite loop ... when Perl detects that a repeated expression matched a zero-length substring.
To break the loop, the following match after a zero-length match is prohibited to have a length of zero.
... the second best match is chosen if the best match is of zero length ... the second-best match is the match at the position one notch further in the string.
Thanks to
ambrus for the pointer to the right section in the docs.