v Match ZIP Codes - Regex

Match ZIP Codes

Source: Regular Expressions Cookbook

Preliminaries

# Load regex package
import re

Create some text

# Create a variable containing a text string
text = '3829 South Ave Street, Pheonix, AZ 34923'

Apply regex

# Find any ISBN-10 or ISBN-13 number
re.findall(r'[0-9]{5}(?:-[0-9]{4})?', text)
['34923']