in reply to help with lazy matching
Think about the regex from left to right. It will match on the first slash, then you tell it to match any characters, and then it must match end-of-string/line. So from the regex engine's point of view, it's completed the match - indeed, you can see this if you run this from the command line: "perl -Mre=debug -wMstrict -le '"/foo/bar/baz/bat"=~/\/(.+?)$/; print $1'". The quickest fix I can think of off the top of my head is to change your dot (.) to [^\/].
The ? would be applicable in the case when your regex wasn't anchored to the end of the string, for example:
$ perl -wMstrict -le '"/foo/bar/baz/bat"=~/\/(.+)\//; print $1' foo/bar/baz $ perl -wMstrict -le '"/foo/bar/baz/bat"=~/\/(.+?)\//; print $1' foo
Also: Is /foo/bar/baz/bat supposed to be a filename? Because if yes, I would really strongly recommend that you use fileparse from File::Basename; there are a few other possible modules but this one is in the core so it should always be available. For example:
use File::Basename 'fileparse'; my $filename = fileparse("/foo/bar/baz/bat"); print "$filename\n"; __END__ bat
And by the way, I think the ? is more commonly referred to as making the expression "non-greedy".
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: help with lazy matching
by nlwhittle (Beadle) on Jan 05, 2015 at 22:19 UTC | |
by AnomalousMonk (Archbishop) on Jan 05, 2015 at 23:09 UTC | |
|
Re^2: help with lazy matching
by Special_K (Pilgrim) on Jan 05, 2015 at 22:17 UTC | |
by nlwhittle (Beadle) on Jan 05, 2015 at 22:27 UTC | |
by Special_K (Pilgrim) on Jan 05, 2015 at 23:07 UTC | |
by davido (Cardinal) on Jan 05, 2015 at 23:51 UTC | |
by Anonymous Monk on Jan 05, 2015 at 22:58 UTC | |
by Anonymous Monk on Jan 05, 2015 at 22:37 UTC |