UI Bakery RegEx Library
About UI Bakery
Try UI Bakery for Free ↗
RegEx library
Email regex Python
Phone number regex Python
IP address regex Python
Date regex Python
URL regex Python
Numbers only regex (digits only) Python
UUID regex Python
Regex match words Python
ZIP code regex Python
GUID regex Python
Password regex Python
HTML regex Python
SSN regex Python
XML regex Python
Mac address regex Python
Street address regex Python

ZIP code regex Python

ZIP code (US postal code) regular expression can be used to verify if a given string contains a valid ZIP code or extract ZIP code from a string. Supports both 5-digit and 9-digit (ZIP+4) formats.

Discover UI Bakery – an intuitive visual internal tools builder. Try it now!

JavaScript
Python
Java
C#
PHP

ZIP code regex

A regular expression to test a string against ZIP code format:

"^[0-9]{5}(?:-[0-9]{4})?$"
Test it!
/^[0-9]{5}(?:-[0-9]{4})?$/

True

False

Enter a text in the input above to see the result

Example code in Python:

import re

# Validate ZIP code
words_pattern = "^[0-9]{5}(?:-[0-9]{4})?$"
re.match(words_pattern, '80001') # Returns Match object
re.match(words_pattern, '80001-2222') # Returns Match object
re.match(words_pattern, '800010') # Returns None

# Extract ZIP code from a string
words_extract_pattern = "[0-9]{5}(?:-[0-9]{4})?"
re.findall(words_extract_pattern, 'My zip code is 80001') # returns ['80001']
Test it!

True

False

Enter a text in the input above to see the result

Test it!

True

False

Enter a text in the input above to see the result

Notes on ZIP code validation and extraction

This ZIP code regex has several limitations:

  • It only works for US ZIP codes. If you need to support other countries, you might need to have a separate regular expression for each one of them and execute it based on the country provided.
  • It can not guarantee that ZIP code actually exists. For instance, 99999 is a correct format, but this ZIP code does not exist.
  • The extraction method can generate false-positive extraction if a string contains multiple numbers.

Create an internal tool with UI Bakery

Discover UI Bakery – an intuitive visual internal tools builder.

Try UI Bakery ↗