An Old Guy and His Dog
An Old Guy and His Dog

Reputation: 499

Regex for matching state and postal code

I need a regular expression that matches the following strings in a big block of text:

California 94558
California 94558-0000
North Dakota  88888
ND  88888-8888

Double and single spaces are necessary between state and zip.

So far I have this:

/([a-zA-Z]+(\s+[a-zA-Z]+))\s+(\d{5}([\-]\d{4})?)/g

But I cannot get it to work with single word states. I would also like to add Canada postal codes, but I think I won't confuse things here.

Upvotes: 2

Views: 2176

Answers (3)

Usman
Usman

Reputation: 2029

Try this regex!

[a-zA-Z]+\s?[a-zA-Z]+\s+\d{5}(-\d{4})?

Below is the output of above regex in regex101.

regex output

Upvotes: 0

Jongware
Jongware

Reputation: 22457

You were actually very close. Your regex only needs specifying that the space-2nd word combo is optional. I only added a single ? to make it work:

([a-zA-Z]+(\s+[a-zA-Z]+)?)\s+(\d{5}([\-]\d{4})?)
                        ^ there

A slightly neater way is to suppress capturing all matched groups, and just pick up the two most useful ones:

([a-zA-Z]+(?:\s+[a-zA-Z]+)?)\s+(\d{5}(?:[\-]\d{4})?)

Now Group 1 will always contain the state – one or two words – and Group 2 will always hold the zip code.

See also the regex101 demo.

Upvotes: 2

Mike Tung
Mike Tung

Reputation: 4821

If your always expecting state state zip or state zip

you can break this down to this pattern ^\w+\s+(\w|\d|\s)+-?\d+ with mg flag for multi-line and global regex match.

Upvotes: 0

Related Questions