{"id":5,"date":"2011-01-14T16:24:09","date_gmt":"2011-01-14T16:24:09","guid":{"rendered":"http:\/\/www.morematter.net\/?p=5"},"modified":"2014-02-28T11:45:06","modified_gmt":"2014-02-28T11:45:06","slug":"regular-expression-for-extracting-us-zip-codes","status":"publish","type":"post","link":"https:\/\/experimentalism.net\/morematter\/regular-expression-for-extracting-us-zip-codes\/","title":{"rendered":"Regular Expression for extracting US Zip Codes"},"content":{"rendered":"<p>There are many such expressions out there, but for some reason none of them worked in my particular situation, which was a single line extracted from a multi-line address or a single form field, which may or not be zip+4 format, and which maybe on its own, following a city and\/or state, and maybe preceding a country. This was for a C# web application &#8211; syntax may vary depending on platform.Some example data are as follows:<\/p>\n<ul>\n<li>12345<\/li>\n<li>12345-6789<\/li>\n<li>Anytown, ST 12345<\/li>\n<li>Anytown, ST 123456 (too many numbers! not valid)<\/li>\n<li>Anytown, ST 1234 (not enough numbers! not valid)<\/li>\n<li>Anytown ST, 12345-6789<\/li>\n<li>Anytown, ST 12345 USA<\/li>\n<li>12345 USA<\/li>\n<li>etc.<\/li>\n<\/ul>\n<p>And here is the expression:<\/p>\n<p><code>(^[0-9]{5}(\\-[0-9]{4}){0,1}$)|((?&lt;=[^\\d\\w]+)[0-9]{5}(\\-[0-9]{4}){0,1}$|((?&lt;=[^\\d\\w]+)[0-9]{5}(\\-[0-9]{4}){0,1}(?=\\D)))<\/code><\/p>\n<p>I know that there are still a few flaws &#8211; for example, 12345-678 will return 12345, even though the whole thing is obviously an invalid attempt at zip+4. I&#8217;m not worried about that in this context, but maybe worth improving at some point. And, I&#8217;m pretty sure that this is not the most efficient expression, but it was the best I came up with at the time. Improvements welcome&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Extract a zip from a a single line extracted from a multi-line address or a single form field, which may or not be zip+4 format, and which maybe on its own, following a city and\/or state, and maybe preceding a country<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-5","post","type-post","status-publish","format-standard","hentry","category-regex"],"_links":{"self":[{"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/posts\/5","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/comments?post=5"}],"version-history":[{"count":4,"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/posts\/5\/revisions"}],"predecessor-version":[{"id":44,"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/posts\/5\/revisions\/44"}],"wp:attachment":[{"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/media?parent=5"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/categories?post=5"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/tags?post=5"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}