#!/usr/bin/perl -w
use strict;
my $string = 'anotherwordhere foobar wordhere baz qux wordhere';
$string =~ /anotherwordhere\s+(.*?) wordhere/;
print "Non-greedy match: '$1'\n";
$string =~ /anotherwordhere\s+(.*) wordhere/;
print "Greedy match : '$1'\n";
That prints:
Non-greedy match: 'foobar'
Greedy match : 'foobar wordhere baz qux'
Do you see why it is called non-greedy? It matches as little as it has to in order to get the job done. The normal (greedy) behavior is to match as much as it can (and still get the job done.)
The getting the job done part is what's most important. The distinction between greedy and non-greedy is only significant when there is both a short and a long way to get the job done. If you changed the string in that example so that it ended in "wordnothere" , both would match the same thing.
I hope that clears it up a bit. Some experimentation will probably help your understanding.
-sauoq
"My two cents aren't worth a dime.";
|