4.14. Validate ZIP Codes
Problem
You need to validate a ZIP code (U.S. postal code),
allowing both the five-digit and nine-digit (called
ZIP+4) formats. The regex should match 12345 and 12345-6789, but not
1234,
123456,
123456789, or
1234-56789.
Solution
Regular expression
^[0-9]{5}(?:-[0-9]{4})?$| Regex options: None |
| Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby |
VB.NET example
If Regex.IsMatch(subjectString, "^[0-9]{5}(?:-[0-9]{4})?$") Then
Console.WriteLine("Valid ZIP code")
Else
Console.WriteLine("Invalid ZIP code")
End IfSee Recipe 3.6 for help with implementing this regular expression with other programming languages.
Discussion
A breakdown of the ZIP code regular expression follows:
^ # Assert position at the beginning of the string.
[0-9]{5} # Match a digit, exactly five times.
(?: # Group but don't capture:
- # Match a literal "-".
[0-9]{4} # Match a digit, exactly four times.
) # End the noncapturing group.
? # Make the group optional.
$ # Assert position at the end of the string.| Regex options: Free-spacing |
| Regex flavors: .NET, Java, XRegExp, PCRE, Perl, Python, Ruby |
This regex is pretty straightforward, so there isn’t much to add.
A simple change that would allow you to find ZIP codes within a longer
input string is to replace the ‹^› and ‹$› anchors with word boundaries, so you end up
with ‹\b[0-9]{5}(?:-[0-9]{4})?\b›.
Tip
There is one valid ZIP+4 code that this regex will not match:
10022-SHOE. This is the only ZIP code that includes letters. In 2007, it was assigned ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access