Regular Expressions :: greedy vs. non-greedy

 1     # greedy vs. non-greedy matching
 2     my $string = 'This string has one dog and another dog';
 3    
 4     # .* matches as much as it can (greedy)
 5     if ($string =~ /^(.*)dog/) {
 6         print $1;   # prints 'This string has one dog and another '
 7     };
 8    
 9     # .* matches as little as it needs to yet still match
10     if ($string =~ /^(.*?)dog/) {
11         print $1;   # prints 'This string has one '
12     };
13    
14     # can also assign the match result directly
15     my ($result) = $string =~ /^(.*?)\s+dog/;
16