Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

matching and backreferencing filenames

by ezekieldas (Initiate)
on Sep 30, 2011 at 16:23 UTC ( #928851=perlquestion: print w/ replies, xml ) Need Help??
ezekieldas has asked for the wisdom of the Perl Monks concerning the following question:

Howdy Monks. After many years of being away from the language I'm trying again. This time, I'd like to learn The Right Way. I generally understand this problem (from code I've purloined and beat into shape to suit my own needs) but there are some specifics which confuse me. Now that I know what a backreference is I'd like to better understand (a) how/why to get this match on the files listed below and (b) a more efficient means of matching the extension. Here is the loop (the remainder of comments/questions are in the code):
@files includes (I'd like to match all of these): "__asdf.jpg asdf.jp +g FOO.AVI Hjz-6Nj.JPG this is[myfile].jpg" foreach my $file( @files ) { # I've tried various "character classes (w, W, d, D...), # combinations of anchoring, not anchoring, using + # I'm not getting consistent matches. At best the below # gives me most files but Hjz-6Nj gets "$1 = Hjz" I lose # the hyphen. # # And for $2, when I have a list of extensions like this # is there a sort of array I can place them in? And # what about camel case extensions? This long list seems # really innefficent. if ( $file =~ /(\w+|W).(jpg|JPG|gif|GIF|wmv|WMV|mpg|MPG|avi|AVI)$/ ) + { my $prefix = $1; my $ext = $2; print "prefix: [$prefix]\n"; print "ext: [$ext]\n"; } }

Comment on matching and backreferencing filenames
Download Code
Re: matching and backreferencing filenames
by jethro (Monsignor) on Sep 30, 2011 at 16:46 UTC

    First of all '.' in your regex doesn't only match '.' but almost anything

    If you have one filename per line and each starting at the beginning of the line, you don't need to constrain your search expression with \w, use the more general '.'

    if ( $file =~ /(.+)\.(jpg|JPG|gif|GIF|wmv|WMV|mpg|MPG|avi|AVI)$/ )
Re: matching and backreferencing filenames
by Lotus1 (Chaplain) on Sep 30, 2011 at 17:58 UTC
    ...And # what about camel case extensions? This long list seems # really innefficent. (jpg|JPG|gif|GIF|wmv|WMV|mpg|MPG|avi|AVI)$/

    You can do case insensitive matching with the 'i' modifier after the final matching delimiter.

    (jpg|gif|wmv|mpg|avi)$/i
Re: matching and backreferencing filenames
by ezekieldas (Initiate) on Oct 03, 2011 at 22:06 UTC
    Thanks for the suggestions. I've applied them to my script and they were very helpful points. Cheers!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://928851]
Approved by dsb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-09-21 18:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (173 votes), past polls