Jun 22, 2008

Example on Greedy Operator ' * '

#!F:\Perl\bin\perl -w
use strict;

my $str = "perl is awesome, I am also awesome";
$str =~ /.*awesome/; # '*' is greedy operator, so it is not satisfied with the first occurance
print $&,"\n"; # perl is awesome, I am also awesome

#To restict the greeedyness to first occurance
#Use '?' operator to restrict the greediness
$str =~ /.*?awesome/;
print $&,"\n"; #perl is awesome

1 comment: