in reply to Re: Extracting just the extension from a filename.
in thread Extracting just the extension from a filename.

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.

Replies are listed 'Best First'.
Re: Re^2: Extracting just the extension from a filename.
by dug (Chaplain) on Jul 19, 2002 at 19:44 UTC
    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.