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

Regex for zip files.

by Karger78 (Beadle)
on Jun 09, 2009 at 20:29 UTC ( [id://770089]=perlquestion: print w/replies, xml ) Need Help??

Karger78 has asked for the wisdom of the Perl Monks concerning the following question:

This is a simple one. I am having issues with perl for matching a zip extention at the end of a file. I have two files, one zip and one zip.meta, both seem to be matching, how could I accomplish an if statement that matches one or the other. Here is my code.
if($uploads[$count] =~ m/zip$/i) #/.*(zip)/ { print "I GOT A ZIP"; }else{ print "I got NADA" } if($uploads[$count] =~ /.*(meta)/){ print "I GOT A META" }else{ print "I got NADA" }

Replies are listed 'Best First'.
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" }
      1. /zip$/i matches any filename ending in "zip" in upper or lower case, including filenames without a dot ("zappzip" and "foobar.notazip" would match).
      2. /meta/ matches any filename that contains meta ("metaphysic-illustrations.gif" would match), not only those that end with ".zip.meta".
      3. The first RE is case-insensitiv, the second RE isn't. Why?

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Regex for zip files.
by Your Mother (Archbishop) on Jun 09, 2009 at 21:12 UTC

    All righty then. Give this structure a spin.

    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"; }
Re: Regex for zip files.
by Your Mother (Archbishop) on Jun 09, 2009 at 20:43 UTC

    Not sure what you're really doing but the /zip$/ works-

    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.

      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.

        It's not matching both. Try running my version and looking at the way ikegami broke it down above too. Make sure you are using strict and warnings in your code; they catch tons of simple problems.

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?
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.
      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" } }
      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-20 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found