Direct Mail

Regular Expressions for Street Addresses

By 2 December 2021No Comments
instant global address validation

Regular Expression (RegEx) For Street Address

Have you ever considered using regular expressions while parsing a street address? And did you ever stop to think whether the use of the regular expression for address parsing is a good practice? Well, the truth is that it’s best not to mix regular expressions with address parsing or address validation processes. Unfortunately, many programmers still fall for the same old mistake every time they decide to use regular expressions for street addresses, only to fail.

instant global address validation

For those of you who are still unsure what exactly regular expressions are, also known as “Regex” or “regexp,” it is a sequence of characters that can form a search pattern that can be used for identifying a pattern. Regex is mainly used for string matching or pattern matching with strings. They have this incredible ability to take an insane amount of text to search and identify patterns in them. Furthermore, they can even manipulate or extract the text in specific regions, which makes it extremely resourceful for both scientific and software applications.

This article discusses everything you need to know about using regular expressions for street addresses. We hope to educate you on regular expression and provide you with the information you need to make the right decisions when it comes to regular expressions used for address validation. Furthermore, we also dive into subjects including address regex, componentizing and extracting an address, standardizing an address, and more.

Regular Expressions for Address Validation

We have already mentioned the capability of regex to take any amount of text and identify specific patterns in it. When it comes down to address databases, especially those of businesses, there is a significant variance in both the content and format of the addresses. Addresses are not “regular,” which implies that an address does not have an essential factor in using regex for the information processing. As we discuss regular expressions for address validation in detail, we try to understand how regex works concerning addresses for componentizing, extracting, or standardizing.

Regex works best when they are used in a precisely controlled environment with predictable input and context-free language. But, when it comes down to theoretical and practical applications, there are a lot of limitations faced by regular expressions. These limitations are the reason why the regular expressions are not an ideal fit for parsing or correcting street addresses. As you might guess, the standardized form of address in the US is defined by the USPS, publication 28 to be precise.

The address written according to USPS standards is not in a regular language which means that it can not be expressed in a context-free manner. So, naturally, it means that the addresses can not be parsed by a regular expression. However, you could try and match a minor subset of street addresses with regex, but only if you were to assume that the input is already standardized or typed with the regular format and in the standard format. What is most commonly observed is that the regex has proven itself useful but not sufficient enough to carry out an advanced process like parsing street addresses.

Address Componentizing

Componentizing is an integral part of the address validation process. During componentization, the address is broken up into components. The address in your database may or may not be in a standardized format, it may or may not have punctuations, it may or may not be capitalized, the possibilities are practically limitless. Many developers consider that regex is their only option to get them out of such a situation but, unless all the addresses are in the standardized format, regex can not help you.

As we have mentioned before, Regex looks for patterns in text (address in this case), but, unfortunately, they have no way of identifying what each part of the text means. If we put it in Layman’s term, regex can not tell if “Smartville” or “St. Louisville” is a city name or not. Similarly, there could be other confusion during the componentization process too. Hence, it is clear that you can not parse street addresses effectively with regex.

Address Extraction

Address extraction from text files is an age-old method, and it is especially useful for applications that highlight addresses in a document body. The method is also good if you want to add content into context with respect to a location. That being said, there are often instances where regex falls short and fail to deliver the desired results. As seen in the componentization process, regex runs into trouble when it encounters punctuations, or improper spacings, which can, in turn, deceive the parsers.

The truth is that even if we were to eliminate the punctuations, there is no guarantee that the large data will be made interpretable. In fact, in many situations, there is so much data that it is almost impossible to decipher even by a human. With large data, it becomes increasingly difficult to determine whether part of the data is a street or a plaza or simply just a business name. This again points to the fact that regular expression for address validation is not a good idea.

Address Standardization

We are all aware that the addresses come in various forms as well as styles. Sometimes the addresses may include some unexpected data, including secondary numbers. Sometimes, the address is in a rural route or PO Box address, or sometimes it is even a military address. Ans some other times, an address can contain two addresses commonly known as dual addresses.

Perhaps it is the toughest process to be implemented using regex, and we know this because we have been through this. It is more than likely that every developer who has ever tried to standardize an address using regex found it to be more painful than rewarding. Furthermore, if you were to hardcode, various abbreviations, keywords, etc., will make your regex excruciatingly long and large, and in turn, it becomes inefficient when it comes to compilation and execution.

Alternative for Regex Street Address Validation

Extracting addresses from a block of texts remains an excruciating task using age-old methods like regex. There are much easier ways to parse, standardize, and validate your address database using modern solutions like PostGrid. With an advanced address verification tool like PostGrid, the address is parsed, standardized, and validated within a matter of seconds. Furthermore, there is no hardcoding required to integrate PostGrid’s API into your business website.

Unlike using regular expressions for address validation, PostGrid employs a custom algorithm that makes way for a systematic parsing and interpretation of the addresses. Instead of using a pattern matching method, PostGrid compares the data in hand with USPS’s official address database to ensure that your addresses are valid and deliverable. What’s more, is that even if the whole address is given in a single line, PostGrid’s API can still seamlessly conduct an address verification process.


It is evident that using an age-old method like regular expression for address validation is not a viable option today. With such a large database available, it becomes increasingly difficult and perhaps even impossible. It makes no sense to use such unreliable methods when there are many advanced alternatives available. The best action you can take is to employ an advanced address verification tool like PostGrid that can easily parse, standardize, and verify your addresses within a matter of seconds.

Ready to Get Started?

Start transforming and automating your offline communications with PostGrid