Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Parse out the extension of a filename - return base of filename.

by rob_au (Abbot)
on Mar 12, 2002 at 23:45 UTC ( #151268=note: print w/replies, xml ) Need Help??


in reply to Parse out the extension of a filename - return base of filename.

I was never able to find a decent piece of code that was able to parse out the extension of a given file delimited by a '.' regardless of how many '.'s were in the filename

While I do admire your industrial spirit, you can already do this with File::Basename ...

use File::Basename; my $fname = "/usr/local/isp/system.update.perl"; my ($name, $path, $suffix) = fileparse($fname, '\.[^\.]*'); print STDOUT $name, "\n";

The key to this working is the regular expression passed to the fileparse method - The regular expression in the example above matches the suffix of a file as being the last dot and what follows it, excluding any subsequent dots ([^\.]).

 

perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Replies are listed 'Best First'.
Re^2: Parse out the extension of a filename - return base of filename.
by freonpsandoz (Beadle) on Sep 20, 2016 at 22:26 UTC

    The key to this working is the regular expression passed to the fileparse method

    Too bad File::Basename doesn't set a reasonable default for the fileparse() RE based on the OS like it does for the path separator. If you understand REs well enough to supply a correct one to fileparse(), you most certainly don't need File::Basename to get the file extension!

Re: Re: Parse out the extension of a filename - return base of filename.
by snafu (Chaplain) on Mar 13, 2002 at 12:39 UTC
    Yes. I was aware of the availability of function from File::Basename. However, in order to use it you needed to have prior knowledge of the filename which was being passed to the module ie you needed to know how many periods are in the filename. This wouldn't work very well if you were going through a whole list of files that were not structured the same. That is one reason I wrote this lil sub because it doesn't care how many period '.' delimiters are in the filename. It will always return the full filename minus the last part of the filename which is what I have always considered to be the extension.

    _ _ _ _ _ _ _ _ _ _
    - Jim
    Insert clever comment here...

      Umm.. try running the example he gave (or reading the node more closely). It does match the last part of the filename following the final dot. So if you run:

      perl -MFile::Basename -e 'print join ":", fileparse("foo.bar.baz", "\\ +.[^.]+"), "\n"'

      It produces foo.bar as the filename, ./ as the directory and baz as the extension. A few more filenames and results follow:

      Input filenameBase nameExtension
      foo.bar.bazfoo.barbaz
      foo.bar foo bar
      foo foo none

      -ben

        Yup. I know. Clearly I spoke before I tested and I did a terrible terrible thing and assumed his code wouldn't work. But, alas, I was wrong. :)

        _ _ _ _ _ _ _ _ _ _
        - Jim
        Insert clever comment here...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2022-09-26 13:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (117 votes). Check out past polls.

    Notices?