Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to Grab City, State, Zip

Tags:

regex

ruby

Trying to make a regex that can handle input like either:

  1. Beverly Hills, CA
  2. Beverly Hills, CA 90210

I have this:

^(.+)[,\\s]+(.+)\s+(\d{5})?$

It works for the #2 case, but not #1. If I change the \s+ to \s* then it works for #1 but not #2.

You can play around with it here: http://rubular.com/r/oqKBJ4r8cq

like image 206
Dex Avatar asked Nov 28 '25 09:11

Dex


2 Answers

Try this:

^(.+)[,\\s]+(.+?)\s*(\d{5})?$

http://rubular.com/r/qS0e5vAQnT

like image 74
Dogbert Avatar answered Nov 30 '25 00:11

Dogbert


Try this instead:

^([^,]+),\s([A-Z]{2})(?:\s(\d{5}))?$

This expression works on both examples, captures each piece of the address in separate groups, and properly handles whitespace.

Here is how it breaks down:

^           # anchor to the start of the string
([^,]+)     # match everything except a comma one or more times
,           # match the comma itself
\s          # match a single whitespace character
([A-Z]{2})  # now match a two letter state code 
(?:         # create a non-capture group
    \s        # match a single whitespace character
    (\d{5})   # match a 5 digit number
)?          # this whole group is optional
$           # anchor to the end of the string
like image 24
Andrew Hare Avatar answered Nov 30 '25 00:11

Andrew Hare