Regular Expressions
Global Find
The g flag can find repeated matches in a string.
Global Find
global_find.pl
use strict;
use warnings;
my $text = ;
my $count = 0;
my $last_pos = 0;
while ($text =~ /cat/g) {
$count = $count + 1;
$last_pos = pos($text);
}
print "text=$text\n";
print "count=$count\n";
print "last_pos=$last_pos\n";
use strict;
use warnings;
my $text = ;
my $count = 0;
my $last_pos = 0;
while ($text =~ /cat/g) {
$count = $count + 1;
$last_pos = pos($text);
}
print "text=$text\n";
print "count=$count\n";
print "last_pos=$last_pos\n";
use strict;
use warnings;
my $text = ;
my $count = 0;
my $last_pos = 0;
while ($text =~ /cat/g) {
$count = $count + 1;
$last_pos = pos($text);
}
print "text=$text\n";
print "count=$count\n";
print "last_pos=$last_pos\n";
global match
A global match keeps searching after the previous match.