abhy has asked for the wisdom of the Perl Monks concerning the following question:
my $foo = "bar road"; if ($foo =~ /\b\w+\s*(road)?/) { print "Matched:", $1, "\n"; }
In the above code in $1 road is captured which is as expected.
Now if I change the regex to /\b\w+\s*?(road)?/, that is, make the space matching lazy then "road" is not captured.
Can someone please tell me as to why this happens as soon as I make the space matching lazy?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex lazy behaviour
by GrandFather (Saint) on Mar 03, 2009 at 10:35 UTC | |
|
Re: Regex lazy behaviour
by moritz (Cardinal) on Mar 03, 2009 at 10:29 UTC | |
|
Re: Regex lazy behaviour
by ELISHEVA (Prior) on Mar 03, 2009 at 10:40 UTC | |
|
Re: Regex lazy behaviour
by abhy (Novice) on Mar 04, 2009 at 06:18 UTC |