| Refresh | Home EGTry.com

a string match pattern in multiple occurrences


match one at a time

#match multiple occurrences of pattern
use strict;

my $text=<<"_end";
a multiple line text
the second line
the last line
_end

print "input:\n$text\n";

#match one at a time
print "output words:\n";
while($text =~ /\w+/g) {
print "$&\n";
}



match all occurrences at once, return the list

#match multiple occurances of pattern
use strict;

my $text=<<"_end";
a multiple line text
the second line
the last line
_end

print "input:\n$text\n";


#return all matchs
print "output words:\n";
my @allwords=$text =~ /\w+/g;
print join("\n", @allwords), "\n";