RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find U.S. Street Address
Expression
^(?n:(?<address1>(\d{1,5}(\ 1\/[234])?(\x20[A-Z]([a-z])+)+ )|(P\.O\.\ Box\ \d{1,5}))\s{1,2}(?i:(?<address2>(((APT|B LDG|DEPT|FL|HNGR|LOT|PIER|RM|S(LIP|PC|T(E|OP))|TRLR|UNIT)\x20\w{1,5})|(BSMT|FRNT|LBBY|LOWR|OFC|PH|REAR|SIDE|UPPR)\.?)\s{1,2})?)(?<city>[A-Z]([a-z])+(\.?)(\x20[A-Z]([a-z])+){0,2})\, \x20(?<state>A[LKSZRAP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADL N]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD] |T[NX]|UT|V[AIT]|W[AIVY])\x20(?<zipcode>(?!0{5})\d{5}(-\d {4})?))$
Description
captures US street address. Address format: ##### Street 2ndunit City, ST zip+4 address1 - must have street number and proper case street name. no punctuation or P.O Box #### punctuation manditory for P.O. address2 - optional secondary unit abbr. Secondary range required for some units. City - Proper case city name. State - State abbreviation. All caps zip - zip+4. Can't be all zeroes Abbreviations for secondary units and States are those used by the US Postal Service. http://www.usps.com/ncsc/lookups/usps_abbreviations.html Certain secondary units require a secondary range, see the above link THis RE isn't unbreakable, Probably will allow some false positives but should work for most addresses.
Matches
123 Park Ave Apt 123 New York City, NY 10002 | P.O. Box 12345 Los Angeles, CA 12304
Non-Matches
123 Main St | 123 City, State 00000 | 123 street city, ST 00000
Author Rating: The rating for this expression. Michael Ash
Source
Your Rating
Bad Good

Enter New Comment

Title

Name

Comment

Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: Doesn't work in JavaScript.
Name: LegacyBass
Date: 1/2/2013 12:58:59 PM
Comment:
Even if you remove all the descriptive groups so it's just the RE, JavaScript still can't compile or use this RE.


Title: Blocked people with 6-digit street number
Name: PRMan
Date: 8/21/2009 8:29:33 PM
Comment:
You just blocked people with 6-digit street numbers


Title: Blocked people with 6-digit street number
Name: PRMan
Date: 8/21/2009 8:29:25 PM
Comment:
You just blocked people with 6-digit street numbers


Title: What flavor
Name: RegEx newbie
Date: 6/21/2009 5:12:23 PM
Comment:
It would really be helpful if I knew what flavor of RegEx was this written for.


Title: What flavor
Name: RegEx newbie
Date: 6/21/2009 5:12:05 PM
Comment:
It would really be helpful if I knew what flavor of RegEx was this written for.


Title: What flavor
Name: RegEx newbie
Date: 6/21/2009 4:43:36 PM
Comment:
It would really be helpful if I knew what flavor of RegEx was this written for.


Title: What flavor
Name: RegEx newbie
Date: 6/21/2009 4:42:58 PM
Comment:
It would really be helpful if I knew what flavor of RegEx was this written for.


Title: What flavor
Name: RegEx newbie
Date: 6/21/2009 4:42:21 PM
Comment:
It would really be helpful if I knew what flavor of RegEx was this written for.


Title: What flavor
Name: RegEx newbie
Date: 6/21/2009 4:41:52 PM
Comment:
It would really be helpful if I knew what flavor of RegEx was this written for.


Title: Tried this
Name: noviceRegExer
Date: 1/15/2009 9:21:28 AM
Comment:
This doesn't seem to work. I tested in Expresso and it returns nothing. I even pasted in the matches listed here and it doesn't work.


Title: Re: I understand everything in the regexp except...
Name: Michael Ash
Date: 1/25/2006 4:01:20 PM
Comment:
(?n:) and (?i:) are regex options. They aren't supported by all implementation of regex or have the same syntax. (?:n) is an explicit capture option, meaning only named groups are captured. (?i:) is the ignorecase option.


Title: I understand everything in the regexp except...
Name: Mike Rosile
Date: 1/25/2006 3:20:45 PM
Comment:
I really appreciate the regular expression and I think its great! I've been looking for something like this for a while now ... Didn't want to have to write it from scratch ;-) I understand everything in the regular expression except the (?n: and (?i: in front of the two name captures (?<address1> and (?<address2>. Could you elaborate on those? -TIA


Title: Re: It doesnt work for me
Name: Michael Ash
Date: 10/18/2004 10:06:21 AM
Comment:
Most likely the named groups (?<address1>...) are not supported or the syntax is different if it is. If you do not need the address parts you can simply remove all the names fields (the question mark, the angle brackets and the text between them)


Title: It doesnt work for me
Name: Aaron
Date: 10/15/2004 10:11:24 PM
Comment:
I am trying to use it with preg_match(), and it keeps giving me errors. EX> Warning: Compilation failed: unrecognized character after (?< at offset 6 in /home/fallen/public_html/work/contactSpidey.php on line 637. Any idea why its doing that?


Title: Update
Name: Michael Ash
Date: 12/10/2003 12:03:23 AM
Comment:
Updated RE to capture address parts. Also adding check for optional secondary unit Changed the separator to allow spaces or new line


Title: More Detail
Name: Michael Ash
Date: 11/20/2003 7:53:06 AM
Comment:
This RE accepts most US Addresses in format ### street city, State zip. It doesn't account for secondary Address lines or units (ie Apt ## or Suite ###) Street and City names must be in proper case State Abbreviation in all Caps No puncuation in street names. Period allowed after first part of city


Title: Multiline version
Name: Michael Ash
Date: 11/20/2003 1:51:40 AM
Comment:
This is the original version of this RE I wrote ((\d{1,5}(1\/[234])?( [A-Z]([a-z])+)+)|(P\.O\. Box \d{1,5}))\r\n([A-Z]([a-z])+(\.?)( [A-Z]([a-z])+){0,2})\, (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]) (?!0{5})\d{5}(-\d{4})?$ But I couldn't add it the to library because the sample matches can't accept <CR> although the test expression box can The only difference in this expression and the one submitted is the \r\n is replace by a space so sample cold be entered One more note: a different RE parser would reject the input with \r\n but would accept \n or \s This lib wouldn't match on \n or \s but would also take \s{2}


Copyright © 2001-2024, RegexAdvice.com | ASP.NET Tutorials