Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: A better way? Extracting filename from url

by Parham (Friar)
on Jan 14, 2002 at 16:28 UTC ( #138556=note: print w/ replies, xml ) Need Help??

in reply to A better way? Extracting filename from url

i use these with local harddrive filenames, but they should work pretty much the same for url's

method 1:
$file = substr $url, rindex($url, '/') + 1; #path contains full url

method 2:
$url =~ s/^.*\///; #just use the url to get the filename with a regex

method 3:
($file) = $url =~ m!([^/]+)$!; #another regex way

Comment on Re: A better way? Extracting filename from url
Select or Download Code
Replies are listed 'Best First'.
Re: Re: A better way? Extracting filename from url
by moodster (Hermit) on Jan 14, 2002 at 17:01 UTC
    It works, but I guess you've also got to consider the possibility that the URL will contain query parameters, like this:

    So a second substitution (I'm sure you can do it with a lookahead assertion or similar, but that seems a bit overkill) is probably in order:

    $file =~ s/\?.*//;

    -- moodster

      Moodster has a good point. Thanks all for your comments. To be honest I had not even considered the possiblity of query paramters, which is very silly of me. But hey, it was late. Anway, I can now look at this again a bit later on.
      I am constantly amazed and impressed with the good nature and high value of comments that come out of this site.

      The caffeine addict (now sufficiently supplied).

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2015-10-05 04:38 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (107 votes), past polls