469,915 Members | 2,697 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,915 developers. It's quick & easy.

How to match zipcode for simple address matching regex?

I am trying to make a regex to match simple addresses like

123 Main Street
Simpleville, VA 12345

I have little experience with regex's and through reading and lots of messing around I have come up with.

Expand|Select|Wrap|Line Numbers
  1. /(\d{1,5}).*\b(A[LKSZRAP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY])\b/gm
  2.  
While this can get many false positives it seems to work well enough in most cases since the state abbreviation has to be uppercase. The problem I am having it matching the zipcode too. What I did was attach \d{5}? to the end of that regex but that just caused the whole regex to fail. Shouldn't the ? mean that it will optionally match 5 digits if it sees them? Thanks.
Jan 31 '11 #1

✓ answered by Oralloy

dschu012,

I don't see anything in your pattern looking for a zip code.

Perhaps try adding something like this bit at the end of your pattern:
Expand|Select|Wrap|Line Numbers
  1.  *[0-9]{5,5}(-[0-9]{4,4})?
Which is to say:
  1. one or more spaces
  2. followed by five digits
  3. optionally followed by a hyphen and four additional digits

Sorry if I'm a little old-school, I don't know all your requirements. If you look on CPAN, there are some really good (Perl) patterns for recognizing all sorts of things.

For example, Geo::StreetAddress::US.

Cheers!
Oralloy

3 2120
Oralloy
983 Expert 512MB
dschu012,

I don't see anything in your pattern looking for a zip code.

Perhaps try adding something like this bit at the end of your pattern:
Expand|Select|Wrap|Line Numbers
  1.  *[0-9]{5,5}(-[0-9]{4,4})?
Which is to say:
  1. one or more spaces
  2. followed by five digits
  3. optionally followed by a hyphen and four additional digits

Sorry if I'm a little old-school, I don't know all your requirements. If you look on CPAN, there are some really good (Perl) patterns for recognizing all sorts of things.

For example, Geo::StreetAddress::US.

Cheers!
Oralloy
Jan 31 '11 #2
Thanks. I got what I needed by adding
Expand|Select|Wrap|Line Numbers
  1. (\s+\d{5})?
  2.  
to the end. Previously I had the ? inside the parenthesis with was causing the regex to come back with nothing if there were no spaces after the state.
Jan 31 '11 #3
Oralloy
983 Expert 512MB
Yep, much more concise.

So you don't have to deal with nine-digit zip codes, then?
Jan 31 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

17 posts views Thread by Andrew McLean | last post: by
6 posts views Thread by Talin | last post: by
5 posts views Thread by olaufr | last post: by
3 posts views Thread by Mario | last post: by
9 posts views Thread by c_beginner | last post: by
6 posts views Thread by PaulM | last post: by
5 posts views Thread by Alan M Dunsmuir | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.