DebugPointer
Published on

Regex for US Post Code validation

Regex for US Post Code validation

In the United States, a postal code is a series of digits that is used to identify a specific geographical area or address. Postal codes are used in the US to help the postal service efficiently sort and deliver mail to the correct address. They are also known as postcodes or ZIP codes. In this article let's understand how we can create a regex for PIN Code and how regex can be matched for PIN Code.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

Structure of US Post Code

  • Postal codes in the US are made up of five digits.
  • The digits are used to identify a particular postcode area, district, sector, or unit.
  • A United States postal code, for example, might be either five digits or five digits followed a hyphen (dash) and another four digits (12345-1234)

Regex for checking if US Post Code is valid or not

Regular Expression-

/^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))$/gm

Test string examples for the above regex-

Input StringMatch Output
sd21w2does not match
54332matches
213245does not match
43211-2224matches

In case you want to skip validating the space inbetween, you can use the following regular expression-

/^([a-zA-Z]{1,2}\d{1,2})?(\d[a-zA-Z]{2})$/gm

Here is a detailed explanation of the above regex-

/^([a-zA-Z]{1,2}\d{1,2})\s*?(\d[a-zA-Z]{2})$/gm

^ asserts position at start of a line
1st Capturing Group ((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))
1st Alternative (\d{5}-\d{4})
2nd Capturing Group (\d{5}-\d{4})
\d matches a digit (equivalent to [0-9])
{5} matches the previous token exactly 5 times
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
\d matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
2nd Alternative (\d{5})
3rd Capturing Group (\d{5})
\d matches a digit (equivalent to [0-9])
{5} matches the previous token exactly 5 times
3rd Alternative ([A-Z]\d[A-Z]\s\d[A-Z]\d)
4th Capturing Group ([A-Z]\d[A-Z]\s\d[A-Z]\d)
Match a single character present in the list below [A-Z]
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
\d matches a digit (equivalent to [0-9])
Match a single character present in the list below [A-Z]
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
\d matches a digit (equivalent to [0-9])
Match a single character present in the list below [A-Z]
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
\d matches a digit (equivalent to [0-9])
$ asserts position at the end of a line
Global pattern flags
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
g modifier: global. All matches (don't return after first match)

Hope this article was useful to match US Post code number regex pattern.