in reply to help with lazy matching
In theory, you could get around that and use a non-greedy quantifier by first reversing the string and then reversing the result, with this:
But that's a bit ugly and unnatural.$ perl -E 'my $c = reverse "/foo/bar/baz/bat"; say "matched ", scal +ar reverse ($1) if $c =~ m{(.+?)/};' matched bat
The alternative if to forget about non-greedy quantifier for such a case and use character class, as in the old days where there was no non-greedy quantifier:
or:$ perl -E 'my $c = "/foo/bar/baz/bat"; say "matched $1" if $c =~ m{/ +(\w+)$};' matched bat
Please also note how I used { } as regex delimiters (theare are many others that you can use, most non-letter or number characters, such as [] (), ##, §§,etc.) so that I did not have to escape the /.$ perl -E 'my $c = "/foo/bar/baz/bat"; say "matched $1" if $c =~ m{/( +[^/]+)$};' matched bat
It was not really mandatory in such a simple example, but it often makes life easier when you have slashes in your regex.
Update: modified the first code snippet which did not reverse the result. Thanks to AnomalousMonk for pointing out the mistake.
|
|---|