naterockhold Posted September 9, 2011 Share Posted September 9, 2011 I'm trying to create a regular expression to match the following types of us locations: 486 S SOANGETAHA RD APT 9 GALESBURG IL 450 N CHERRY ST GALESBURG IL 61401 950 REDWOOD SHORES PKWY UNIT K102 REDWOOD CITY CA 123 MAIN ST 123 MAIN ST SAN FRANCISCO MAIN ST & KELLOGG ST EMBARCADERO ST & MARKET ST SAN FRANCISCO These locations are in a long string with that varies in length. This is the regular expression I'm using but it is also matching other strings that are not a location. $STATE="(A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[sY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|PA|RI|S[CD]|T[NX]|UT|V[AT]|W[AIVY])"; $STREET="(AVE|AVENUE|BLVD|BOULEVARD|CIR|CIRCLE|CT|COURT|EXPY|EXPRESSWAY|FWY|FREEWAY|LN|LANE|PKY|PKWY|PARKWAY|RD|ROAD|SQ|SQUARE|ST|STREET|TPKE|TURNPIKE)"; $DIREC="(N|NORTH|E|EAST|S|SOUTH|W|WEST|NE|NORTHEAST|SE|SOUTHEAST|SW|SOUTHWEST|NW|NORTHWEST)"; $NAME="(([A-Z]{3,15})( +([A-Z]{3,15})){0,1}( +([A-Z]{3,15})){0,1})"; $DESIG="(APARTMENT|APT|BUILDING|BLDG||FLOOR|FL|SUITE|STE|UNIT|ROOM|RM|DEPARTMENT|DEPT)"; $NUM="([0-9]{1,6})"; $APT="([A-Z0-9]{1,6})"; $ZIP="([0-9]{5})"; $USAddressPtn = "($NUM{0,1} *$DIREC{0,1} *$NAME{1} *$STREET{1} *$DESIG{0,1} *$APT{0,1} *$NAME{0,1} *$STATE{0,1} *$ZIP{0,1})"; Quote Link to comment https://forums.phpfreaks.com/topic/246812-regular-expression-to-find-a-us-postal-address-or-cross-streets/ Share on other sites More sharing options...
requinix Posted September 9, 2011 Share Posted September 9, 2011 You do realize you're asking for a nearly impossible answer, right? Unless you have a database of all addresses then you cannot get a 100% accurate solution. Looking for indicators like ST/Street and LN/Lane are good but that's only a start. How many words are in the name of the street? You don't know. If you find a "IN" is that a preposition or an abbreviation? Is "HI" a greeting or a state name? What about "CIRCLE"? Walk 100 paces south until you reach a busy street. Step 3. Draw a three inch circle in the large square provided. Quote Link to comment https://forums.phpfreaks.com/topic/246812-regular-expression-to-find-a-us-postal-address-or-cross-streets/#findComment-1267521 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.