Home >> perlexamples >>
How to count number of matches in regex?
my $string = "111AAABBBAAACCCDDDEEE";
# method 1:
my $counter1 = 0;
$counter1++ while ($string =~ m/AAA/g);
# method 2:
my $counter2 = ($string =~ s/AAA/AAA/g);
note: If you don't have the /g at the end, it will restart the search from the beginning each time and you'll have an infinite loop.
Perl functions
|
handling Errors in perl How to include module in perl? Perl Data Dumper perl: How to parse config file
Download perl
download stable version of perl
Perl 5.10.1
|
|