Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Extracting file name from path

by HTTP-404 (Sexton)
on Jun 14, 2001 at 08:02 UTC ( [id://88288]=perlquestion: print w/replies, xml ) Need Help??

HTTP-404 has asked for the wisdom of the Perl Monks concerning the following question:

Dear freidnds i have following string
c:\downloads\perl\test.cgi
i need to get the file name out of path i have following patternt, but it doesn't work
/^.*\\(\w+\.w{3})$/
thank you very much

Replies are listed 'Best First'.
Re: Extracting file name from path
by ZZamboni (Curate) on Jun 14, 2001 at 08:26 UTC
    As bikeNomad said, you are missing a backslash before the w for matching the extension. The following should work:
    /.*\\(\w+\.\w{3})/;
    Also take a look at File::Basename and File::Spec, which have different methods for splitting and massaging paths and filenames.

    --ZZamboni

Re: Extracting file name from path
by Arguile (Hermit) on Jun 14, 2001 at 08:51 UTC

    Isn't a regexp going a little overboard for a pattern like this?

    my $str = 'c:\dir\subdir\filename.ext'; my $filename = (split/\\/, $str)[-1];

    Grabs everything after the last \ and I'm willing to bet much faster (and it's easier to read than that regexp). If I had retained more of the context talks in 'Camel' I seem to remeber you can drop the [-1] by getting it to still be in list context when assigned to $filename (instead of $filename getting the scalar 4) but I can't find the refference.

Re: Extracting file name from path
by bikeNomad (Priest) on Jun 14, 2001 at 08:13 UTC
    That's not working because it's looking for three w's at the end of the name. If you don't have to validate the file name, why not just something like:
    m{([^\\/]+)$}
Re: Extracting file name from path
by tachyon (Chancellor) on Jun 14, 2001 at 10:39 UTC

    You could make it a little more portable by allowing / as well with:

    my $str = 'c:\dir\subdir/filename.ext'; my $filename = ( split /\\|\//, $str )[-1];

    but then of course you have a regex in there, oh well.

    tachyon

Re: Extracting file name from path
by Chady (Priest) on Jun 14, 2001 at 10:58 UTC

    That said.. always try to avoid the dot star in your regexes, because they are greedy.. but if you have to use them.. u can minimize it by the ? like that: /^.*?\\(\w+\.w{3})$/


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
      Actually, a dot star can be quite useful, and is considerably faster than using the non-greedy version. In this example, using dot star makes it easy, e.g.,
      /^.*\\(.*)/
      That will extract all the information after the last \. Using the .*? version it would have to be:
      /.*?\\(?!.*\\)$/
      This would aslo make it a lot slower.

      However, trying to find the last \, like in this example, could probably be made simpler by a call to reverse. Though, in the example of extracting a filename, definately you should use a module like File::Basename, as aforementioned. This way it will be portable accross systems, and it is tested for errors, so it will save you valuable time trying to fix a regex that has something wrong with it(and errors always slip in, as do special cases).

        Good advice dimmesdale, same with all the rest. But one point:

        However, trying to find the last \, like in this example, could probably be made simpler by a call to reverse

        Close, but why not rindex. Take this example:

        $file = substr($path, 1 + rindex($path,'\\'));
        It finds the last \ in the string and asigns everything after that.

        That said, I would definately use something like File::Basename if you plan on using this script more than once.

        The 15 year old, freshman programmer,
        Stephen Rawls

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://88288]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (None)
    As of 2024-04-25 04:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found