View Full Version : ZIP & Postal Code matching Regex

johnnyb

04-02-2007, 09:45 PM

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.

aedrin

04-02-2007, 09:48 PM

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/

johnnyb

04-02-2007, 10:06 PM

Honestly, I haven't tried anything so far, however, here are a couple of ideas, (time for a laugh):

// 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

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:

$pattern = '#[0-9]{5}([ -])?(4[0-9]{3})?#'

Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.