← Back to challenges

Regex Series: 5-Digit Zip Code

PythonHardstringsregex

Instructions

Write a regular expression that matches a string if and only if it is a valid zip code.

Examples

"32554" ➞ True

"92 342" ➞ False
# Invalid: contains a whitespace

"9@342" ➞ False
# Invalid: contains a non-numeric character

"923444" ➞ False
# Invalid: length is not 5

Notes

  • Zipcodes must be 5 digits long exactly and only contain numbers.
  • Not allowed: non-numeric characters or whitespaces.
  • This challenge is designed to use Regex only.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Letter Occurrences Per Word