Regexdrill

Patterns for common data Updated November 2026

Regex for ZIP code

US ZIP codes match ^\d{5}(?:-\d{4})?$ : five digits with an optional dash and four more (ZIP+4). Other countries differ: UK postcodes need ^[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}$ with the i flag, Canada ^[A-Z]\d[A-Z] ?\d[A-Z]\d$, Germany ^\d{5}$.

Syntax

/^\d{5}(?:-\d{4})?$/

Examples

["90210", "90210-1234", "9021", "90210 1234"].map(s => /^\d{5}(?:-\d{4})?$/.test(s))

Result: [true, true, false, false]

/^[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}$/i.test("SW1A 1AA")

Result: true

UK postcode: outward code, optional space, inward code.

/^[A-Z]\d[A-Z] ?\d[A-Z]\d$/i.test("K1A 0B1")

Result: true

Canadian postal code.

"Austin, TX 78701-2345".match(/\b\d{5}(?:-\d{4})?\b/)[0]

Result: "78701-2345"

Unanchored with word boundaries to pick the ZIP out of an address.

Common mistakes

  • \d{5} without anchors matches inside 123456; add ^ and $ or \b on both sides.
  • Converting ZIPs to numbers drops the leading zero of 02134; keep them as strings.
  • Using (-\d{4})? creates a capture you do not need; (?:-\d{4})? keeps group numbering clean.
  • Allowing a space instead of a dash in ZIP+4 by accident with [- ]; the official separator is a hyphen.

Practice Regex for ZIP code

These drills check your answer on live data, free, no account needed.

  • US ZIP and ZIP+4The whole line must match. Match a five-digit ZIP code, optionally followed by a hyphen and four more digits.
  • Counted repeats: 3 to 5 digitsThe whole line must match. Match lines made of 3, 4 or 5 digits and nothing else.
  • Optional group: phone extensionThe whole line must match. Match a number like 555-0142, optionally followed by a space, ext, a space and 1 to 4 digits.

Questions

What is the regex for ZIP+4?

^\d{5}-\d{4}$ requires the extension; ^\d{5}(?:-\d{4})?$ makes it optional, which is what most forms want.

Is there one regex for all postal codes?

No. Formats differ per country (digits only, letters and digits, variable length). Pick the pattern per country from the address form, or accept ^[A-Z0-9 -]{3,10}$ with the i flag as a loose catch-all.

What is the regex for a Canadian postal code?

^[A-Z]\d[A-Z] ?\d[A-Z]\d$ with the i flag. Canada Post excludes the letters D, F, I, O, Q and U; a stricter version replaces [A-Z] with [ABCEGHJ-NPRSTVXY] for the first letter and [ABCEGHJ-NPRSTV-Z] for the others.

Related

  • Regex for numbers onlyThe ^\d+$ pattern for digits-only strings, stripping everything that is not a digit, extracting numbers from text, and patterns for signed and decimal values.
  • Optional with ?How ? makes a character optional, making a whole sequence optional with a group, what the capture holds when the group is absent, and the ? that means lazy.
  • Non-capturing group (?:...)How (?:...) groups for quantifiers and alternation without a numbered capture, why that keeps group numbers stable, and how it changes findall in Python.
  • Regex for phone numberPhone number regex for US numbers with dashes, dots, spaces or parentheses, an E.164 check for international input, and how to normalize before validating.

Previous: Regex for passwordNext: Regex for hex color