tags:

views:

211

answers:

3

so im looking for a regex or some solution to detect street address, phone, fax etc in western countries.

i know it wont be perfect, but still my priority is on US and Canadian street addresses, province/state, postal code and etc....

it would be nice if someone went out and did this already, instead of me rewriting the regex...

thank you.

A: 

Using information I got from this question I searched http://regexlib.com/ and found what you are looking for

This matches either postal code or zip

^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z] \d[A-Z]\d$

Phone or fax:

^\+[0-9]{1,3}\([0-9]{3}\)[0-9]{7}$

Like Ben has mentioned, you won't be able to verify whether or not the address is valid or not, but you can verify that the format is correct.

Nathan Koop
A: 

Hi,

You can probably find some interesting stuff in the sub-packages of PEAR::Validate (it's in PHP) that correspond to the locales you want

For instance, in the Validate_US class :

function postalCode($postalCode, $strong = false)
{
    return (bool)preg_match('/^[0-9]{5}((-| )[0-9]{4})?$/', $postalCode);
}

The same method, in the Validate_FR class :

function postalCode($postalCode, $strong = false)
{
      return (bool) preg_match('/^(0[1-9]|[1-9][0-9])[0-9][0-9][0-9]$/', 
                               $postalCode);
}


But note that this kind of regex will only allow you to validate that an given code looks valid, not that it actually is valid : there are so many postal codes (and even more addresses !), the list would be un-manageable, and a maintenance nightmare, I guess.

Pascal MARTIN
A: 

Canadian postal codes can be verified though Canada Post's website.

It returns a range of valid addresses given a postal code. I'm not sure if there's a web API for it, but it could provide much better accuracy than a regex.

Ben S