Skip to content

Instantly share code, notes, and snippets.

@fatgy
Created December 16, 2010 05:56
Show Gist options
  • Save fatgy/743092 to your computer and use it in GitHub Desktop.
Save fatgy/743092 to your computer and use it in GitHub Desktop.
Regular Expression that splits the last line in an address into it's funtional parts.
//http://wiki.cdyne.com/index.php/City_State_Zip_Regex
^(?<city>[A-Za-z\s]*)(?:,\s?(?<state>[\sA-Za-z]+)|\s(?<state>[A-Za-z]{2}))
(?:
(?:\d{1,4})|
(?<zip>\d{5})(?:-\d{0,3})|
(?<zip>\s\d{5})-(?<zip4>\d{4})|
(?<zip>\s\d{5})|
(?<pc>\s[AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d[A-Za-z]\s?\d[A-Za-z]\d)|
)$
///This regular expression will return the following elements of an address:
//* City
// * State/Province (called State in the return)
// * Zip
// * Zip4 - The Zip4 part of the zipcode.
// * PC - This is the Postal Code for Canadian Addresses.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment