24

image Recognizing U.S. ZIP Codes

Matching U.S. ZIP codes for syntactic correctness is straightforward. They are composed from a string of numeric characters. Any nonnumeric character is an error, except for the dashes in ZIP+4 codes.

To match the standard 5-digit ZIP code we could use this regular expression:

[0-9]{5}

The “+4” part of the extended ZIP code is a variation with a “4” in the braces. Combined, we get this for the whole ZIP+4 combination with its embedded dash:

[0-9]{5}\-[0-9]{4}

This will also match a ZIP code with too many digits in either half. We need to delimit the ZIP code and allow either of the two forms. In this case, we ...

Get Developing Quality Metadata now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.