Got more questions? Find advice on: ASP | SQL | XML | Windows
Welcome to RegexAdvice Sign in | Join | Help

validating addresses that are missing information

  •  01-07-2009, 8:15 AM

    validating addresses that are missing information

    I have been working with php and regular expressions to separate string addresses into their components, and I have come across a problem...

    Normally, addresses follow the format:
    [Address], [City], ON, [Postal Code]
    I am only dealing with addresses in Canada, specifically in Ontario (hence the ON)

    I was using preg_match and the regular expression I was using to separate them is:
    $Address = '/([\w|\s]*),([\w|\s]*),[\s]*ON[\s]*(\w\d\w \d\w\d)/';

    So, with preg_match($Address, $stringAddress, $matches), $matches[1] would be the address, $matches[2] would be the city, $matches[3] would be the postal code, and the province would always be ON and the Postal Code format is always \w\d\w \d\w\d.

    This was working fine until I came across some addresses that had some information missing, including the COMMAS, which made it hard to match...

    Since I know that there are only a certain number of possible cities (let's say Toronto and Scarborough) I want to write the following expression

    (the ADDRESS which is anything EXCEPT 'ON', 'Toronto', 'Scarborough', COMMA, or a Postal Code(\w\d\w \d\w\d) IF it exists) THEN
    [COMMA if it exists] THEN
    (Toronto OR Scarborough IF it exists) THEN
    [COMMA if it exists] THEN
    (ON if it exists) THEN
    (Postal Code if it exists)

     So, it should be able to match:

    121 Steeles Ave, Scarborough
    121 Steeles Ave Scarborough ON
    121 Steeles Ave Toronto ON
    121 Steeles Ave Toronto ON L3R 3E5
    121 Steeles Ave, Toronto, ON, L3R 3E5
    Toronto, ON
    ON L3R 3E5

    I have tried many regexs but NOTHING works...I am not very good at getting the NOT operator (^) to work for me...

    Filed under:
View Complete Thread