in reply to Extract string from rear of string

Something like
$FileName = "/var/log/apache/error.log"; $FileName =~ m/\.(\w*)$/; $Extension = $1;
should do the trick. In this code fragment I'm looking for a real period, followed by any number of word characters, followed by the end of the string. I put that in brackets so that I can grab it as $1 later on.

This of course makes the assumption that your definition of a file extension is the group of characters to the right of the last period in a file name .. so the file extension of "foo.bar.baz" would be "baz". Your code should also handle the situation where there are no periods in the filename.

ps I highly recommend reading Programming Perl (Third Edition) by Wall, Christiansen & Orwant, published by O'Reilly.

"Excellent. Release the hounds." -- Monty Burns.

Replies are listed 'Best First'.
Re: Re: Extract string from rear of string
by Ven'Tatsu (Deacon) on Dec 28, 2001 at 08:58 UTC
    Your really should not use \w as many file systems will allow characters other than alpha-numerics. For instance 'file.$#@%' would be a valid name but would break your code.