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

I need to match valid US addresses. So far I am using this regex to grab the city and zip, but I need to exclude any ten-digit zip codes. Thanks, guys. <code>/\W(a-zA-Z{2})\W\s*(\d{5}-\d{4}|\d{9}|\d{5}))/<\code>

Replies are listed 'Best First'.
Re: regexp
by chromatic (Archbishop) on Sep 11, 2000 at 21:43 UTC
    What does your input look like? I'd do something like this:
    my $input = "Holland, MI 82904-4413"; my ($city, $state, $zip) = split(' ', $input); $city =~ s/,$//; bad_zip($zip) unless ($zip =~ /^\d{5}(-)?(\d{4})?$/);
    Optimize as necessary.
Re: regexp
by swiftone (Curate) on Sep 11, 2000 at 22:03 UTC
    Your regexp looks fine. The only trick is that it will match anywhere in the string, so a ten digit zip will match with 9 digits. If your string ends you can put $ into your regexp (end of string/line), otherwise you can match whitespace
      I noticed that it would match the first 9 out of 10. I'm trying to make it as flexible as possible to make a module for work. I suppose I should just add a \W+$ at the end of it.