Backreferencing and greed : Index « Regular Expression « Perl






Backreferencing and greed

    
$fruit="apples pears peaches plums";

$fruit =~ /(.*)\s(.*)\s(.*)/;
print "$1\n";
print "$2\n";
print "$3\n";
print "-" x 30, "\n";
$fruit="apples books";
$fruit =~ /(.*?)\s(.*?)\s(.*?)\s/; # Turn off greedy quantifier
print "$1\n";
print "$2\n";
print "$3\n";

   
    
    
    
  








Related examples in the same category

1.Back referencing
2.Backreferencing and greedy quantifiers
3.Match the first alphanumeric character
4.Matched index
5.$1 gets 'A', $2 gets' B'
6.Regex index