portfoliobuilder
portfoliobuilder

Reputation: 7856

How to parse street address with Regex?

I am working on a regex to parse full addresses by street number, street name, city, state, and zip code.

I came up with a pretty good regex that works for most cases, however, there are a couple scenarios where it fails. I need help with improving it. Here is what I have currently

Pattern pattern = Pattern.compile("^([\\d-]{0,}[\\s-]{0,}[\\d/]+)[\\s]{0,}");

This works fine if the street addresses are formed nicely where the address starts with a street number that has no letters attached to it. For example :

I have done a lot of research on parsing addresses and this solution I have come up with is just about the simplest solution I've found. Just need a little more tweaks. Thanks in advance.

Upvotes: 1

Views: 1404

Answers (1)

Al Wang
Al Wang

Reputation: 354

You shouldn't break down all street addresses into one regular expression. You're better off handling street addresses with multiple regular expressions to cover a wide range of scenarios e.g.

  • 123 Stackoverflow Way
  • 5000 5th Avenue
  • 1 Hacker Way Building 5

Upvotes: 1

Related Questions