scoopseven
scoopseven

Reputation: 1777

Python Regex Zipcode Matching

I have this regex that matches zipcodes in northern California. I'd like to get them into a single regex; is this possible?

(re.match(r'9[0-2].+', zip) or re.match(r'93[0-5].+', zip))

I've tried:

re.match(r'9[0-2|3[0-5].+', zip)

but this doesn't work.

Upvotes: 2

Views: 1587

Answers (2)

CanSpice
CanSpice

Reputation: 35788

I'm a little puzzled as to why you're using a . in your regex. . matches any character, and as far as I know zip codes can only contain numbers.

I think you actually want (9[0-2]\d{3}|93[0-5]\d\d).

Edit: Alternatively, 9([0-2]\d|3[0-5])\d\d for a shorter regex.

Upvotes: 3

David Robinson
David Robinson

Reputation: 78590

re.match(r'9([0-2]|3[0-5]).+', zip)

Upvotes: 5

Related Questions