May 2009
Intermediate to advanced
510 pages
15h
English
You need to validate a ZIP code (U.S. postal code), allowing both
the five-digit and nine-digit (ZIP + 4)
formats. The regex should match 12345 and 12345-6789, but not 1234, 123456, 123456789, or
1234-56789.
^[0-9]{5}(?:-[0-9]{4})?$| Regex options: None |
| Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby |
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.5 for help with implementing this regular expression with other programming languages.
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.
? # Repeat the preceding group between zero and one time.
$ # Assert position at the end of the string.| Regex options: Free-spacing |
| Regex flavors: .NET, Java, PCRE, Perl, Python, Ruby |
This regex is pretty straightforward, so there isn’t much to
add. A simple change that allows 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›.