in reply to Count number of occurrences of a substr

Here are two ways of counting matches:

$n++ while $str=~/this/g; $str =~ /this(?{$n++})(*F)/;

Replies are listed 'Best First'.
Re^2: Count number of occurrences of a substr
by ikegami (Patriarch) on Sep 28, 2011 at 18:53 UTC
    Note, $n needs to be a local our $n; instead of my $n; for the second snippet to work properly in a sub.