0

Fill in the code to check if the text passed includes a possible U.S. zip code, formatted as follows: exactly 5 digits, and sometimes, but not always, followed by a dash with 4 more digits. The zip code needs to be preceded by at least one space, and cannot be at the start of the text.

import re

def check_zip_code (text):
    result = re.search(r"___", text)
    return result != None
print(check_zip_code("The zip codes for New York are 10001 thru 11104.")) # True
print(check_zip_code("90210 is a TV show")) # False
print(check_zip_code("Their address is: 123 Main Street, Anytown, AZ 85258-0001.")) # True
print(check_zip_code("The Parliament of Canada is at 111 Wellington St, Ottawa, ON K1A0A9.")) # False

Replace the dash ____ with the answer. Here is mine "[0-9][0-9][0-9][0-9][0-9].*[0-9][0-9][0-9][0-9]" This solution is absolutely right and works fine. Anyone find an alternate solution for this problem because I don't think so. Writing [0-9] this so many time there might be a better solution.

Kota Mori
  • 6,510
  • 1
  • 21
  • 25

1 Answers1

1
def check_zip_code(text):
    return re.search(r'\d{5}(?:-\d{4})?', text) != None

I use exactly 5 digits ({n}) followed by an non-capturing group ((?: ..)) which is optional due to the question mark.

Detlef
  • 6,137
  • 2
  • 6
  • 24