# How to Match a Zip Code in Python using Regular Expressions

In this article, we show how to match a zip code in Python using regular expressions.

So there are really 2 ways to represent zip codes in the United States.

One way is just simply with a 5-digit number, such as 22456.

Another way to represent a zip code is by a 5-digit number, followed by a dash (-), followed by 4 digits. An example of this is 22311-7803.

So in this article, we show how to match a zip code in either xxxxx format or xxxxx-xxxx format.

The code below shows how to do this.

So let's now go over this code.

re is the module in Python that allows us to use regular expressions. So we first have to import re in our code, in order to use regular expressions.

After this, we have a variable, string1, which contains a bunch of valid and invalid zip codes.

We then have a variable, regex, which is set equal to, re.compile(r"(\b\d{5}-\d{4}\b|\b\d{5}\b\s)")

First, you see we have boundaries (\b).

What boundaries does is it allow a digit to appear to the left of a 5-digit number. Thus, numbers greater than 5 digits are excluded. Thus, numbers such as 123456 and 123456789 are excluded, since they have greater than 5 digits.

We then have, \d{5}, which means the number must be 5 digits in length.

This is then followed by a dash (-) and \d{4}

After this, we add another boundary (\b) so that the 4-digit extension cannot have more than 4 digits.

We then have an OR operator (|)

This is followed by \d{5}\b\s

This allows us to select just a 5-digit zip code that doesn't have a 4-digit extension.

And this is all that is needed to match a zip code in Python using regular expressions.

Related Resources

How to Randomly Select From or Shuffle a List in Python