How to find a US zip code in a string using regular expression?

Question:

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.

Couldn’t produce the required output.

import re
def check_zip_code (text):
  result = re.search(r"w+d{5}-?(d{4})?", 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
Asked By: Bharath Bushan

||

Answers:

You could use

(?!A)bd{5}(?:-d{4})?b

Full code:

import re

def check_zip_code (text):
    m = re.search(r'(?!A)bd{5}(?:-d{4})?b', text)
    return True if m else False

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

Meanwhile I found that there’s a package called zipcodes which might be of additional help.

Answered By: Jan
import re


def check_zip_code (text):
    return bool(re.search(r" (bd{5}(?!-)b)| (bd{5}-d{4}b)", text))


assert check_zip_code("The zip codes for New York are 10001 thru 11104.") is True
assert check_zip_code("90210 is a TV show") is False
assert check_zip_code("Their address is: 123 Main Street, Anytown, AZ 85258-0001.") is True
assert check_zip_code("The Parliament of Canada is at 111 Wellington St, Ottawa, ON K1A0A9.") is False

assert check_zip_code("xn90201") is False
assert check_zip_code("the zip somewhere is 98230-0000") is True
assert check_zip_code("the zip somewhere else is not 98230-00000000") is False

Answered By: Terry Spotts

try this code you will get

import re
def check_zip_code (text):
  result = re.search(r"s+d{5}-?(d{4})?", text)
  return result != None
Answered By: Divya Goswami
import re
def check_zip_code (text):
    result = re.search(r" d{5}|d[5]-d{4}", text)
    return result != None
Answered By: shihab sikder
import re
def check_zip_code (text):
  result = re.search(r"d{5}[-.d{4}]", 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

Try this one

Answered By: Emre Kılıç

I think this covers all the conditions
result = re.search(r"sd{5}?(-d{4})?", text)

Answered By: Dnyanesh Dasare

The first character must have a space.
This is an example of this problem only

r"[s][d]{5}"
Answered By: Sejun Jang

This code works perfect and generates required output.

import re

   def check_zip_code (text):

   result = re.search(r" d{5}(-d{4})?", 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
True
False
True
False
Answered By: Swaran
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.