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";
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.