## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# 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 (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

```If Regex.IsMatch(subjectString, "^[0-9]{5}(?:-[0-9]{4})?\$") Then
Console.WriteLine("Valid ZIP code")
Else
Console.WriteLine("Invalid ZIP code")
End If```

### Other programming languages

See Recipe 3.5 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.
?         #   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`.

Recipes 4.15, 4.16, and 4.17

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required