in reply to Rexgrep
This works because the first .* is greedy and matches up until the last "/" in $img; then we keep the rest.if ($img =~ m#.*/(.*)#) { $name = $1; } else { # didn't match! }
However, solutions using substr and index (or rindex) are often faster than regexps in cases like this, and this is no exception, at least with the regexp above:
givesuse Benchmark; timethese(1000000, { 'regexp' => sub { my($file) = $img =~ m#.*/(.*)# }, 'substr' => sub { my $file = substr $img, rindex($img, '/') + 1 } });
Benchmark: timing 1000000 iterations of regexp, substr... regexp: 11 secs (11.18 usr 0.00 sys = 11.18 cpu) substr: 4 secs ( 5.67 usr 0.00 sys = 5.67 cpu)
|
|---|