gw1500se Posted February 1, 2019 Share Posted February 1, 2019 I am struggling to come up with a regexp for verifying a typical US address line of the form: City , ST zip Since City can have spaces, . and ' I need to include that in my regexp. The state should be 2 upper case letters (I don't care if it is really a correct abbreviation) followed by a zip code. Here is what I think it should be and I am asking for verification. [a-zA-Z \.'], [A-Z]{2} [0-9]{5}? TIA. Quote Link to comment https://forums.phpfreaks.com/topic/308256-regexp-for-typical-us-address/ Share on other sites More sharing options...
requinix Posted February 1, 2019 Share Posted February 1, 2019 There are probably also cities with hyphens in the name. So I wouldn't even try to validate the name: just make sure something is there, then a comma, the state and zip. Quote Link to comment https://forums.phpfreaks.com/topic/308256-regexp-for-typical-us-address/#findComment-1564146 Share on other sites More sharing options...
gw1500se Posted February 1, 2019 Author Share Posted February 1, 2019 So is this right? .*, [A-Z]{2) [0-9]{5}? Quote Link to comment https://forums.phpfreaks.com/topic/308256-regexp-for-typical-us-address/#findComment-1564147 Share on other sites More sharing options...
requinix Posted February 1, 2019 Share Posted February 1, 2019 .* will allow there to be nothing. Should be .+ If this is supposed to match the entire address line then use ^ and $ anchors (if not somehow already implied) and add support for zip+4 codes. Quote Link to comment https://forums.phpfreaks.com/topic/308256-regexp-for-typical-us-address/#findComment-1564148 Share on other sites More sharing options...
gw1500se Posted February 1, 2019 Author Share Posted February 1, 2019 There won't be any plus 4 zips. Not sure what you mean with the anchors. .+ , [A-Z]{2) [0-9]{5}? Quote Link to comment https://forums.phpfreaks.com/topic/308256-regexp-for-typical-us-address/#findComment-1564149 Share on other sites More sharing options...
requinix Posted February 2, 2019 Share Posted February 2, 2019 Without the anchors then your regex will only check that the string contains something that matches it. Like there could be stuff after the zip code. With the anchors (or at least the end-of-string $ anchor) you make sure the entire string matches and not just a part of it. Quote Link to comment https://forums.phpfreaks.com/topic/308256-regexp-for-typical-us-address/#findComment-1564152 Share on other sites More sharing options...
gw1500se Posted February 2, 2019 Author Share Posted February 2, 2019 (edited) Oh, I thought the ? did that. Does the ? make the zip optional? So it should be: ^.+[ ]*,[ ]*[A-Z]{2} [0-9]{5}$ I'm not sure that the first [ ]* is not redundant. Edited February 2, 2019 by gw1500se Quote Link to comment https://forums.phpfreaks.com/topic/308256-regexp-for-typical-us-address/#findComment-1564156 Share on other sites More sharing options...
requinix Posted February 2, 2019 Share Posted February 2, 2019 It's not so much redundant as unnecessary. In order, your regex will match: The beginning of the string At least one character Zero or more spaces A comma Zero or more spaces Two capital letters A space Five digits The end of the string Beginning of string + at least one character doesn't really do much because the engine will start matching at the beginning anyways and you're not requiring that the string start with anything in particular. In fact the ^.+[ ]* together are only saying that there must be at least one character before the comma. You could simply the whole thing to just .,[ ]*[A-Z]{2} [0-9]{5}$ which will ensure there is a character before the comma, possible spaces after the comma, the state, a space, and the zip code. Thing is that still doesn't require there to be a city name - it could match the string " , NY 12345". The regex should require at least one non-space character before the comma, keeping in mind that spaces before or after the city name should be allowed. Quote Link to comment https://forums.phpfreaks.com/topic/308256-regexp-for-typical-us-address/#findComment-1564167 Share on other sites More sharing options...
Zane Posted September 29, 2019 Share Posted September 29, 2019 /^(.+),\W?([A-Z]{2})\W?(\d{5})$/g Quote Link to comment https://forums.phpfreaks.com/topic/308256-regexp-for-typical-us-address/#findComment-1570047 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.