Click here to Skip to main content
15,881,882 members
Articles / General Programming / String

Validate and Find Addresses with RegEx

Rate me:
Please Sign up or sign in to vote.
4.55/5 (12 votes)
6 May 2015CPOL2 min read 86.2K   13   7
Learn how to use regular expressions to find addresses or parts of an address in a given string. These patterns can also be used to verify that a given string is a true address.


There may come a time when you have a document full of various pieces of data, possibly one that contains information on a long list of people, but you only need the addresses. This is where RegEx comes in handy. You can use RegEx to iterate through the characters of the document and locate a string that matches the pattern you specify. RegEx can also be used to check a short string to see if its format and contents match a specified pattern. For a detailed reference on RegEx, check out this article.

Using the Code

To start, the easiest piece of the address to match is the zip code although it's the least exact.

A simple pattern to match a zip code would look like the following:

ZIP Code


That pattern matches five primary digits and allows the option of having a hyphen and four extended digits. This matches all zip codes, however it is possible for there to be a match of five digits that is not a zip code. Adding to our pattern will fix that.

Next, we need to match a city and state. This pattern will match most cities:


(?:[A-Z][a-z.-]+[ ]?)+

There is room for false matches with this pattern too but when we add the state pattern, it will be much more accurate.

The only sure way to test for a state name is to create a pattern that contains the name of each state. It's long, but you can always know it's 100% accurate. 


Minnesota|Mississippi|Missouri|Montana|Nebraska|Nevada|New[ ]Hampshire|New[ ]Jersey|New[ ]Mexico
|New[ ]York|North[ ]Carolina|North[ ]Dakota|Ohio|Oklahoma|Oregon|Pennsylvania|Rhode[ ]Island
|South[ ]Carolina|South[ ]Dakota|Tennessee|Texas|Utah|Vermont|Virginia|Washington|West[ ]Virginia

I've added line breaks for readability but make sure to remove those when using the pattern. This is a sure fire way to test for states that are spelled out but in some addresses the states are abbreviated.

State Abbreviations


When you are searching for a city, state, zip combination, combine the patterns like this:

City, State, Zip

{city pattern},[ ](?:{state pattern}|{abbrev. state pattern})[ ]{zip pattern}

To finish off our address RegEx pattern, we need to test for a street address.


\d+[ ](?:[A-Za-z0-9.-]+[ ]?)+(?:Avenue|Lane|Road|Boulevard|Drive|Street|Ave|Dr|Rd|Blvd|Ln|St)\.?

For the full pattern, combine the city, state, zip pattern with this street pattern separated by \s to test for either a space or a line break and you're done!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Web Developer JBHworks
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionHow would you get the Zip/Postal code if the string contains a comma Pin
gwittlock27-Apr-17 22:03
gwittlock27-Apr-17 22:03 
QuestionToo much validation? Pin
PeejayAdams8-May-15 3:37
PeejayAdams8-May-15 3:37 
AnswerRe: Too much validation? Pin
FrostedSyntax8-May-15 6:12
FrostedSyntax8-May-15 6:12 
GeneralHelpful Information Pin
MayurDighe8-May-15 2:37
professionalMayurDighe8-May-15 2:37 
GeneralNice Pin
Member 112198807-May-15 6:02
Member 112198807-May-15 6:02 
QuestionMy Vote 5 Pin
Murat Yalvaq6-May-15 20:55
Murat Yalvaq6-May-15 20:55 
AnswerRe: My Vote 5 Pin
dpminusa8-May-15 16:10
dpminusa8-May-15 16:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.