in reply to regex to return file extensions

I'm assuming that you've got the filename isolated already. If not, the File::Basename module is great. Then to get the extension, use something like this:

print "$1\n" if $filename =~ m/\.([^.]+)$/;


Dave

Replies are listed 'Best First'.
Re: Re: regex to return file extensions
by Hofmator (Curate) on Apr 02, 2004 at 09:22 UTC
    When you are already using File::Basename to extract the filename, then the following is also an option
    ($name, $path, $suffix) = fileparse('/home/foo/bar.txt', qr{[^.]*}); # or if you have a list of allowed suffices in @suf @suf = qw/gif wav mpeg txt/; ($name, $path, $suffix) = fileparse('/home/foo/bar.txt', @suf); # both lead to # $name eq 'bar.' # $path eq '/home/foo' # $suffix eq 'txt'

    -- Hofmator