In the United Kingdom, a postal code is a series of letters and/or digits that is used to identify a specific geographical area or address. Postal codes are used in the UK to help the postal service efficiently sort and deliver mail to the correct address. They are also known as postcodes or ZIP codes (though these terms are not used in the UK). 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 UK Post Code
- Postal codes in the UK are made up of two to four letters, followed by one to four digits.
- It should not start with an alphabet.
- The letters and digits are used to identify a particular postcode area, district, sector, or unit – it has outward postcode and inward postcode.
- Each postcode consists of two parts. The first part is the outward postcode, or out-code.
- This is separated by a single space from the second part, which is the inward postcode, or in-code.
- The outward postcode enables mail to be sent to the correct local area for delivery.
Regex for checking if UK Post Code is valid or not
Regular Expression-
/^([a-zA-Z]{1,2}\d{1,2})\s*?(\d[a-zA-Z]{2})$/gm
Test string examples for the above regex-
Input String | Match Output |
---|---|
33 vv 22 | does not match |
Bs31JP | matches |
000121 | does not match |
bs3 1jp | matches |
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})$/gmi
Here is a detailed explanation of the above regex-
/^([a-zA-Z]{1,2}\d{1,2})\s*?(\d[a-zA-Z]{2})$/gmi
^ asserts position at start of a line
1st Capturing Group ([a-zA-Z]{1,2}\d{1,2})
Match a single character present in the list below [a-zA-Z]
{1,2} matches the previous token between 1 and 2 times, as many times as possible, giving back as needed (greedy)
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case insensitive)
\d matches a digit (equivalent to [0-9])
{1,2} matches the previous token between 1 and 2 times, as many times as possible, giving back as needed (greedy)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
*? matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
2nd Capturing Group (\d[a-zA-Z]{2})
\d matches a digit (equivalent to [0-9])
Match a single character present in the list below [a-zA-Z]
{2} matches the previous token exactly 2 times
a-z matches a single character in the range between a (index 97) and z (index 122) (case insensitive)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case insensitive)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
Hope this article was useful to match UK Post code number regex pattern. In the United Kingdom, postal codes play a crucial role in efficient mail delivery. Understanding the structure and validation of these codes is essential. This article delved into the creation and application of regular expressions (regex) to validate UK postal codes. Regex offers a powerful tool for pattern recognition and manipulation in programming, making it an invaluable asset in various applications.