Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #10601

Re: Zip Codes ctype? Pregmatch?

Message-ID <2791655.Q3g5IOY8jz@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-08-21 14:53 +0200
Subject Re: Zip Codes ctype? Pregmatch?
Newsgroups comp.lang.php
References <kv08v0$614$1@speranza.aioe.org> <kv0vfd$g9t$1@dont-email.me>

Show all headers | View raw


Norman Peelman wrote:

> On 08/20/2013 01:27 PM, Twayne wrote:
>> I'm attempting to check for US and Canadian zip codes (postal codes).
>> The US is easy; mostly just be sure it's five numerics and except
>> "00000" and "99999". But Canadian is a different story because:
>>     It consists of alternating alpha and numeric characters (AnAnAn) but
>> not the entire alphabet. 8 N.A. English letters are not used, as in
>> DFIOQUW AND Z or put another way, they only use 18 letters in their
>> postal codes.
>>     I haven't see a single example in all my research to check if the
>> 1st, 3rd, and 5th characters are alpha and th 2nd, 4th and 6th
>> characters are numeric.
>>
>> I've tried preg_match and strpos without succees, likely due to my own
>> weakness with preg_match, and regex creates an incredibly long statement
>> I'm sure it's not right to put upon the servers; they slow down even my
>> local server XAMPP & PHP 5.3 on win 7.
>>
>> Might anyone have a better method?
>>
>> Or know of any functions anywhere that could be modified to be used?
> 
> US Zip code:
> [0-9]{5}(-{0,1}[0-9]{4}){0,1}
  ^^^^^     ^^^^^^^^^^    ^^^^^
In Perl-Compatible Regular Expressions (PCRE), as also used by PHP's preg_* 
functions, the following shorthands are available:

  - “*” for “{0,}”
  - “?” for “{0,1}”
  - “+” for “{1,}”
  - “\d” for “[0-9]” (includes more numeric characters in “UTF-8 mode”)

Thus, the above expression can be simplified to

  \d{5}(-?\d{4})?

However, the specification above says that “00000” and “99999” are _not_ 
valid U.S. ZIP codes, so to be exact you cannot just use either “[0-9]{5}” 
or “\d{5}”; but you would have to use, for example, a zero-width negative 
lookahead:

  $possibleZips = array('00000', '00001', '99998', '99999');
  foreach ($possibleZips as $possibleZip)
  {
    preg_match('^(?![09]{5})\\d{5}(?:-?\\d{4})?$', $possibleZip, $matches);
    var_dump($possibleZip);
    var_dump($matches);
  }

(thanks to Anubhava: <http://stackoverflow.com/a/9609624/855543>)
 
> Canadian zip code (all one line, don't miss the space!):
> ([A-C,E,G-H,J-N,P,R-T,V,X,Y]{1}[0-9]{1})[A-C,E,G-H,J-N,P,R-T,V,X,Y]{1}
       ^                      ^^^^^^^^
> {1}([0-9]{1}[A-C,E,G-H,J-N,P,R-T,V,X,Y]{1}[0-9]{1})

“{1}” is superfluous in all regular expression flavours (in BRE the escaped 
variant is superfluous).  An expression that matches, matches exactly one 
time unless a following quantifier says otherwise.

In a character class expression, ranges are _not_ delimited by comma.
A comma there is a *literal* comma instead (just like most other special 
characters lose, and “-” gains meaning), and repetitions are ignored:

  [A-C,E,G-H,J-N,P,R-T,V,X,Y]

matches the same strings as

  [A-CEG-HJ-NPR-TVXY,]

So unless you want to allow commas in ZIP codes, you need to remove them 
from the respective character class.

Thus, the above expression would have to be changed, and can be simplified 
to

  ^(?:[A-CEG-HJ-NPR-TVXY]\d){3}$

(The “^” makes sure that the second, fourth, aso. character must be a digit.  
Let \s* follow it if you want to allow leading whitespace.  Likewise for “$” 
and trailing whitespace.)

Anyhow, if an expression is repeated, and this repetition cannot be handled 
with a quantifier like above, in programming languages like PHP that allow 
this, code is easier readable if you assign the repeated expression to a 
variable, and have the variable reference expanded:

  $cdn_letter = '[A-CEG-HJ-NPR-TVXY]';
  $pattern = "^{$cdn_letter}\\d{$cdn_letter}\\d{$cdn_letter}\\d\$";

[In certain programming languages, libraries like my JSX:regexp.js [1] are 
useful that allow you to define and use your own character class escape 
sequences, eliminating the need for variable expansion: "\\p{cdnLetter}".]

Note that expansion/repetition is semantically different from expression 
backreferences:

  $pattern2 = "([A-CEG-HJ-NPR-TVXY])\\d\\1\\d\\1\\d";

$pattern would match "A1B2C3"; $pattern2 would match "A1A2A3", but not 
"A1B2C3".


PointedEars
___________
[1] <http://PointedEars.de/scripts/test/regexp> p.
-- 
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
  -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-20 13:27 -0400
  Re: Zip Codes  ctype? Pregmatch? Martin Leese <please@see.Web.for.e-mail.INVALID> - 2013-08-20 11:59 -0600
    Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-20 15:01 -0400
  Re: Zip Codes  ctype? Pregmatch? Robert Heller <heller@deepsoft.com> - 2013-08-20 13:08 -0500
    Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-20 15:03 -0400
      Re: Zip Codes  ctype? Pregmatch? Robert Heller <heller@deepsoft.com> - 2013-08-20 14:36 -0500
        Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-20 20:17 -0400
  Re: Zip Codes  ctype? Pregmatch? RESOLVED Twayne <nobody@spamcop.net> - 2013-08-20 15:06 -0400
    Re: Zip Codes  ctype? Pregmatch? RESOLVED Norman Peelman <npeelman@cfl.rr.com> - 2013-08-20 19:53 -0400
      Re: Zip Codes  ctype? Pregmatch? RESOLVED Twayne <nobody@spamcop.net> - 2013-08-20 20:29 -0400
        Re: Zip Codes  ctype? Pregmatch? RESOLVED Moon Elf <moonelf@moonelfsystem.net> - 2013-12-16 10:02 +0000
          Re: Zip Codes  ctype? Pregmatch? RESOLVED Doug Miller <doug_at_milmac_dot_com@example.com> - 2013-12-16 12:27 +0000
  Re: Zip Codes  ctype? Pregmatch? Norman Peelman <npeelman@cfl.rr.com> - 2013-08-20 19:52 -0400
    Re: Zip Codes  ctype? Pregmatch? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-21 14:53 +0200
      Re: Zip Codes  ctype? Pregmatch? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-21 14:58 +0200
        Re: Zip Codes  ctype? Pregmatch? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-21 15:06 +0200
          Re: Zip Codes  ctype? Pregmatch? Curtis Dyer <dyer85@gmail.com> - 2013-08-24 10:22 +0000
            Re: Zip Codes  ctype? Pregmatch? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-08-24 12:52 +0200
            Re: Zip Codes  ctype? Pregmatch? Doug Miller <doug_at_milmac_dot_com@example.com> - 2013-08-25 14:31 +0000
              Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-25 13:21 -0400
            Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-25 13:17 -0400
      Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-21 19:10 -0400
        Re: Zip Codes  ctype? Pregmatch? BootNic <bootnic.bounce@gmail.com> - 2013-08-21 22:09 -0400
          Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-22 18:22 -0400
  Re: Zip Codes  ctype? Pregmatch? Jeff North <jnorthau@yahoo.com.au> - 2013-08-21 21:25 +1000
    Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-21 18:53 -0400
      Re: Zip Codes  ctype? Pregmatch? Norman Peelman <npeelman@cfl.rr.com> - 2013-08-21 22:46 -0400
  Re: Zip Codes  ctype? Pregmatch? gordonb.qnlcm@burditt.org (Gordon Burditt) - 2013-08-31 01:17 -0500
    Re: Zip Codes  ctype? Pregmatch? Fiver <numeralfive@gmail.com> - 2013-08-31 16:40 +0200
      Re: Zip Codes  ctype? Pregmatch? Norman Peelman <npeelman@cfl.rr.com> - 2013-08-31 11:56 -0400
        Re: Zip Codes  ctype? Pregmatch? Welsh Vanner <welshvanners@welsh-vanners.co.uk> - 2013-08-31 20:23 +0100
        Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-31 20:21 -0400
      Re: Zip Codes  ctype? Pregmatch? Martin Leese <please@see.Web.for.e-mail.INVALID> - 2013-08-31 09:56 -0600
    Re: Zip Codes  ctype? Pregmatch? Twayne <nobody@spamcop.net> - 2013-08-31 20:07 -0400

csiph-web