Regex for US ZIP codes
A regular expression for five-digit US ZIP codes with an optional four-digit ZIP+4 extension, and the mistakes that catch people out when they store them.
This tool runs in your browser. We never send what you type or paste to a server.
The pattern
/^[0-9]{5}(?:-[0-9]{4})?$/The US Postal Service's addressing standard, Publication 28, writes a ZIP code as five digits, and a ZIP+4 code as those five digits, a hyphen and four more. This pattern accepts both and nothing else.
The tester at the top of this page is loaded with this pattern and the examples below, one per line. It adds the m flag so that ^ and $ apply to each line, and g to show every match. Edit either box to try your own input.
How it works, token by token
| Part | Meaning |
|---|---|
^ | Start of the string. |
[0-9]{5} | Exactly five digits: the ZIP code. |
(?:-[0-9]{4})? | Optionally, a hyphen and four more digits: the ZIP+4 add-on that narrows delivery to a block, building or PO box group. (?:…) groups without capturing, and ? makes the whole group optional. |
$ | End of the string. |
What it matches and what it rejects
| Input | Result | Why |
|---|---|---|
90210 | matches | Five digits. |
02134 | matches | A leading zero is part of the code. |
12345-6789 | matches | ZIP+4 with a hyphen. |
00000 | matches | Accepted: the format does not know which codes are assigned. |
1234 | rejected | Four digits, often a ZIP code that lost its leading zero in a spreadsheet. |
123456 | rejected | Six digits. |
12345- | rejected | A hyphen with nothing after it. |
12345-678 | rejected | The add-on needs four digits. |
12345 6789 | rejected | A space instead of the hyphen. |
123456789 | rejected | Nine digits without the hyphen (see the variant below). |
9021O | rejected | A capital O instead of a zero. |
Store ZIP codes as text
ZIP codes in parts of the north-east of the United States start with 0, such as 02134. A ZIP code is a label, not a quantity, so keep it in a text column. Stored as a number it becomes 2134, and spreadsheets do the same thing when they open a CSV file, which is why four-digit "ZIP codes" turn up in imported data. If you must repair such data, pad it back to five digits, but only for columns you know are US ZIP codes.
Accepting nine digits without a hyphen
Some systems export ZIP+4 as nine digits with no hyphen. Making the hyphen optional accepts both forms:
/^[0-9]{5}(?:-?[0-9]{4})?$/| Input | Result | Why |
|---|---|---|
90210 | matches | Five digits. |
123456789 | matches | Nine digits, no hyphen. |
12345-6789 | matches | With the hyphen. |
1234567 | rejected | Seven digits. |
12345 6789 | rejected | Still no spaces. |
If you accept this form, store a consistent version: insert the hyphen after the fifth digit, or keep the five-digit ZIP code and the add-on in separate fields.
What the format cannot tell you
Not every five-digit number is a ZIP code in use, and the pattern happily accepts 00000. A ZIP code also does not map neatly onto a city or a state: some cross state lines, and many cities have dozens. To check that a ZIP code exists, or to fill in the city and state, use the USPS address tools or an address verification service.
If your form takes addresses from several countries, apply this pattern only when the country is the United States. Canadian postal codes (K1A 0B1) and UK postcodes mix letters and digits, and many countries have no postcode at all, so make the field optional for them.
In JavaScript, Python, PHP and C#
JavaScript
const re = /^[0-9]{5}(?:-[0-9]{4})?$/;
console.log(re.test('90210')); // → true
console.log(re.test('1234')); // → false
Python
import re
pattern = re.compile(r'[0-9]{5}(?:-[0-9]{4})?')
print(bool(pattern.fullmatch('90210'))) # → True
print(bool(pattern.fullmatch('1234'))) # → False
PHP
<?php
$pattern = '/^[0-9]{5}(?:-[0-9]{4})?$/D';
echo preg_match($pattern, '90210'), "\n"; // → 1
echo preg_match($pattern, '1234'), "\n"; // → 0
C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[0-9]{5}(?:-[0-9]{4})?\z");
Console.WriteLine(pattern.IsMatch("90210")); // → True
Console.WriteLine(pattern.IsMatch("1234")); // → FalseEach version checks the whole string. JavaScript's $ only matches at the very end, but Python, PHP and .NET also let $ match just before a final line break, so "abc\n" could slip through. The Python version uses re.fullmatch (so the ^ and $ are dropped), PHP adds the D modifier, and C# ends with \z.
The JavaScript, Python and C# examples are run before each update and their output is compared with the comments. PHP is not installed on our build machine, so the PHP version is checked against the PCRE pattern modifier documentation, and its string literal is checked to hand PHP exactly the pattern tested here.
Questions
What is the regex for a 5-digit ZIP code only?
^[0-9]{5}$ matches exactly five digits. Add (?:-[0-9]{4})? before the $ to also allow ZIP+4.
Should I use \d or [0-9] for ZIP codes?
Use [0-9]. In Python and .NET, \d also matches digits from other scripts, such as Arabic-Indic digits, which are not valid in a ZIP code.
Why do some ZIP codes in my data have four digits?
They were stored or opened as numbers somewhere, and the leading zero was dropped. Keep ZIP codes as text from the start.