in reply to Extracting just the extension from a filename.

$string =~ s/.*\.(.*)$/$1/;
I'm certainly not a regex whiz, but this seems to do what you want. It will handles "."'s within the filename, and gives you the string after the last dot.

HTH,
dug

Replies are listed 'Best First'.
Re^2: Extracting just the extension from a filename.
by Aristotle (Chancellor) on Jul 19, 2002 at 19:29 UTC
    Why are you capturing the extension if you put it back into the string though? This works the same: $string =~ s/^.*[.]//; But it has a problem: for files without an extension, it will return the full filename. If you want to do it with a subsitution rather than a match, it should be $string =~ s/^.*(?:[.]([^.]*))?$/$2/; This way the dot plus extension becomes optional; the base filename is still always erased. But it's so clumsy.. a match is more elegant: my ($extension) = $string =~ /[.]([^.]*)$/;

    Makeshifts last the longest.

      Why are you capturing the extension if you put it back into the string though?

      Too many spare cycles on my machine? Not enough caffine? <grin> Good point.

      Also, thanks for posting the more elegant solution.
        I just had a coke. *grin*

        Makeshifts last the longest.