DevKits

Validation

Match a US ZIP code

Match 5-digit US ZIP codes with optional +4 extension.

Pattern

Regular expression

\b[0-9]{5}(?:-[0-9]{4})?\b

How it works

Matches exactly 5 digits, optionally followed by `-` and 4 more digits. Word boundaries prevent partial matches inside longer numbers.

Matches

  • 94103
  • 10001-1234
  • 20500

Non-matches

  • 1234 (too short)
  • 123456 (too long)
  • 94103-1

Language snippets

JavaScript

const re = /\b[0-9]{5}(?:-[0-9]{4})?\b/g;

Python

re.findall(r"\b[0-9]{5}(?:-[0-9]{4})?\b", text)

Go

re := regexp.MustCompile(`\b[0-9]{5}(?:-[0-9]{4})?\b`)

Java

Pattern.compile("\\b[0-9]{5}(?:-[0-9]{4})?\\b")

Frequently asked questions

What is the regex for US ZIP code?

The pattern \b[0-9]{5}(?:-[0-9]{4})?\b matches US ZIP code. Matches exactly 5 digits, optionally followed by `-` and 4 more digits. Word boundaries prevent partial matches inside longer numbers.

What does this US ZIP code pattern match?

It matches strings like 94103, 10001-1234, 20500, but rejects 1234 (too short), 123456 (too long).

How do I use this pattern in JavaScript, Python, Go, Java?

Ready-to-run snippets are provided for JavaScript, Python, Go, Java. Copy the one for your language from the Language snippets section — each wraps the same core pattern in that language's regex API.

Related patterns

Try this pattern in a tool

Try any pattern live in the Regex Tester