in reply to Re: Re: Req Expression translation
in thread Req Expression translation
Sometimes an example helps...
That prints:#!/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";
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.)Non-greedy match: 'foobar' Greedy match : 'foobar wordhere baz qux'
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.";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Req Expression translation
by Anonymous Monk on Jun 14, 2003 at 17:16 UTC |