Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts

    ZIP & Postal Code matching Regex

    Hi,

    I am in need of two regular expressions that will match American ZIP codes and Canadian postal codes.

    I need to match a US ZIP code either with or without the +4 extension and a Canadian postal code with or without a space between the parts.

    So, a US code can be "90210" or "90210-4256" or maybe even "90210 4256" or "902104256"

    And a Canadian code can be "R3C 4P9" or "R3C4P9" or maybe "R3C-4P9"

    I'm really terrible with regular expressions, if someone could help me with this that would be great.
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    I'm really terrible with regular expressions, if someone could help me with this that would be great.
    What have you tried so far?

    The fact that you are terrible at them, should be an indication that you need to read up on them.

    http://www.regular-expressions.info/

  • #3
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Honestly, I haven't tried anything so far, however, here are a couple of ideas, (time for a laugh):
    PHP Code:
    // us ZIP code
    // I think this will match a 5-digit ZIP, a 5+4 zip with a dash, and a 5+4 where they're jammed together. 
    //I don't know how to add an option to say dash or space
    // Also, there must be a way to say "match x 5 times" instead of writing it out.
    ([0-9])([0-9])([0-9])([0-9])([0-9])(-)?(([0-9])([0-9])([0-9])([0-9]))?

    // Canadian
    // should match with or without a space.  I'm not sure if I entered the space correctly.  I have the same option problem as above
    ([a-z][A-Z])([0-9])([a-z][A-Z])( )?([0-9])([a-z][A-Z])([0-9]) 
    I'll read up on them a bunch at the link you gave. It looks like it may actually be a good regex website, something that I've been having problems finding.

    John
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #4
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    To talk you through the first one:

    It's made up of 3 parts.
    a) 5 numbers (compulsory)
    b) a hyphen or a space (optional)
    c) 4 numbers (optional) (is 'beginning with 4' a restriction? I'm not from the US)

    5 numbers can be expressed as:
    [0-9]{5}

    fairly straight forward? Numbers inside []s like that indicate a range, the curly brackets are for repetition.

    an optional hyphen or space:
    ([ -])?

    Bit more complicated; ()s are used for groupings. The ? at the end indicates that what is inside the brackets is optional. i.e. "it should occur 0 or 1 times"

    [ -] is the same notation as for the numbers above, but signifies "any of the characters included", so a space or a hyphen.

    4, optional, numbers combines some of these bits:
    ([0-9]{4})?
    or, if it does have to begin with 4 then:
    (4[0-9]{3})? (a (4, followed by 3 numbers) 0 or 1 times)

    as a combined expression then:
    PHP Code:
    $pattern '#[0-9]{5}([ -])?(4[0-9]{3})?#' 
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •