Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Fetch filename only

by serf (Chaplain)
on Feb 10, 2006 at 19:14 UTC ( #529439=note: print w/ replies, xml ) Need Help??


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:

$name =~ s/^.*\\//;
(which says remove any characters up to and including the last backslash)

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:

$name =~ s/^[A-Z]:\\.*\\//;
NB(2): Think about the fact that the path will have word characters and spaces and backslashes in it,

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.


Comment on Re: Fetch filename only
Select or Download Code
Replies are listed 'Best First'.
Re^2: Fetch filename only
by Anonymous Monk on Feb 10, 2006 at 20:40 UTC
    Thanks alot for the quick responses.
    We have Perl 5.8 on our server and I assume File::Basename is a standard module with that version?

    If not I will use your req expression but right now I am testing it on my workstation and not sure if we have File::Basename on our server if it is not a standard module.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://529439]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (17)
As of 2015-07-28 18:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (258 votes), past polls