Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Extract string from rear of string

by talexb (Chancellor)
on Dec 28, 2001 at 08:36 UTC ( [id://134807]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-20 15:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found