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".
In reply to Re: help with lazy matching
by Anonymous Monk
in thread help with lazy matching
by Special_K
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |