in reply to Fetch filename only
File::Basename is the most robust and portable way to do this.
If you still wanted to do it via regexp instead you could do something like:
(which says remove any characters up to and including the last backslash)$name =~ s/^.*\\//;
NB(1): This uses =~, not = as you have in your example.
In your example, by using \w\:\\ you were trying to cater for the ones which start with a drive letter [CG]:\,
but were forgetting about \\\InforServer\data.xls which presumably has a UNC pathname, starting with \\
You could have done the "drive letter" ones with:
$name =~ s/^\w:\\.*\\//; (Note: The : doesn't need escaping here)
or better:
NB(2): Think about the fact that the path will have word characters and spaces and backslashes in it,$name =~ s/^[A-Z]:\\.*\\//;
Also all of these regexps *have* to finish with a \\ to terminate the match, otherwise the greediness will go on and eat up the filename as well.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Fetch filename only
by Anonymous Monk on Feb 10, 2006 at 20:40 UTC |
In Section
Seekers of Perl Wisdom