Re: Regex for zip files.
by ikegami (Patriarch) on Jun 09, 2009 at 20:40 UTC
|
You could refine your patterns such that both can't match, or you could use an elsif.
if ($uploads[$count] =~ /zip$/i) {
print "I GOT A ZIP";
}
elsif ($uploads[$count] =~ /meta/) {
print "I GOT A META"
}
else {
print "I got NADA"
}
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
Re: Regex for zip files.
by Your Mother (Archbishop) on Jun 09, 2009 at 21:12 UTC
|
my @files = qw( zip.meta some.zip whoops.txt meta.zip );
for my $file ( @files )
{
if ( $file =~ /\.zip\z/ )
{
process_zip($file);
}
elsif ( $file =~ /\bzip\.meta\z/ ) # or maybe \.meta\z/
{
process_meta($file);
}
else
{
warn "Don't know what to do with '$file'";
}
}
sub process_zip {
my $zip = shift;
print "process_zip got '$zip'\n";
}
sub process_meta {
my $meta = shift;
print "process_meta got '$meta'\n";
}
| [reply] [d/l] |
Re: Regex for zip files.
by Your Mother (Archbishop) on Jun 09, 2009 at 20:43 UTC
|
use warnings; # Don't leave out!
use strict; # This neither!
my @files = qw( zip.meta some.zip );
for my $file ( @files )
{
print $file, "\n";
print $file =~ /zip$/ ? "\tZip!\n" : "\tNada y pues nada...\n";
}
I don't know exactly what you're doing but you always keep in mind that trusting user data or even file extensions is a mistake. Test/validate. Just because it ends in .zip doesn't mean it's a zip file. So you might want to ask about your final goal instead of your code difficulty with the matches.
| [reply] [d/l] |
|
The thing is though it's matching both. Where I want it to match one or the other when it's going though the array.
| [reply] |
|
| [reply] |
|
Re: Regex for zip files.
by JavaFan (Canon) on Jun 09, 2009 at 21:51 UTC
|
I'm confused. Cutting and pasting your code and using a zip and a zip.meta file, neither file matches both regexes:
my @uploads = qw(foo.zip bar.zip.meta);
foreach my $count (0, 1) {
if($uploads[$count] =~ m/zip$/i)
#/.*(zip)/
{
print "I GOT A ZIP\n";
}else{
print "I got NADA\n"
}
if($uploads[$count] =~ /.*(meta)/){
print "I GOT A META\n"
}else{
print "I got NADA\n"
}
}
__END__
I GOT A ZIP
I got NADA
I got NADA
I GOT A META
So, what's your real problem? | [reply] [d/l] |
Re: Regex for zip files.
by Karger78 (Beadle) on Jun 09, 2009 at 21:08 UTC
|
Ok here is the issue. That one code posted doesn't match anything.
My array contains zip files and zip.meta files. With my orignal code it matches the zip on the first pass, then on the 2nd it matches zip and zip.meta.
I would like it to match zip if it's just a zip at the end, or just match zip.meta if that's found within the array. | [reply] |
|
As a suggestion, try this:
my @zips = grep { /\.zip$/} @uploads;
my @meta = grep {/zip\.meta$/} @uploads;
# "Your Mother" pointed out that I had meta/$ instead
# of the correction shown. .meta$/} Oooops.
print "zips= @zips\n";
print "zipmeta= @meta\n";
Update: Something like this might work.
foreach my $upload (@uploads)
{
if ($upload =~ m/zip$/i)
{
print "I GOT A ZIP\n";
}
elsif ($upload =~ m/zip\.meta$/i)
{
print "I GOT A META\n"
}
else
{
print "I got NADA"
}
}
| [reply] [d/l] [select] |
|
If that's the case, you need to provide a sample of your data and show what's not working.
To disagree, one doesn't have to be disagreeable - Barry Goldwater
| [reply] |