Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to get area and district codes from UK postcodes

Tags:

regex

mysql

Basically, I need a regex that would match first sequence of letters in UK postcode (easy) and another regex which would match the following number (not so easy).

Examples of possible combinations and matches:

AA9A 9AA    regex1: AA    regex2: 9
A9A 9AA     regex1: A     regex2: 9
A9 9AA      regex1: A     regex2: 9
A99 9AA     regex1: A     regex2: 99
AA9 9AA     regex1: AA    regex2: 9
AA99 9AA    regex1: AA    regex2: 99

Spaces in poscodes are only there for clarity, they might or might not be there, therefor must be disregarded.

I could use some help with these two regular expressions, especially regex2 (matching the number). By the way, I'm gonna be running this in MySQL 5.6

UPDATE: Just to note: last 3 characters will always be ignored no matter what, that's the only pattern that always applies - it should be visible from my examples.

UPDATE2: Sorry if I wasn't clear enough before - I don't need to validate the post codes, I need to extract area and district codes.

UPDATE3: Just to emphasize again: white space must be disregarded, it could be AA99 9AA or AA999AA - in both cases the regex2 (extract number) should be 99

UPDATE4: Thanks for all your answers, unfortunately I just found out that MySQL can't extract regex matches. Way to go, MySQL, proving to be useless yet again.

like image 773
Caballero Avatar asked Oct 01 '13 13:10

Caballero


People also ask

What is regex postcode?

A simple postcode regular expression, or postcode regex, checks the general shape of the postcode is correct.

How do you read UK postcodes?

Each postcode consists of two parts. The first part is the outward postcode, or outcode. This is separated by a single space from the second part, which is the inward postcode, or incode. The outward postcode enables mail to be sent to the correct local area for delivery.

How do you layout a postcode?

Postcodes should always be in BLOCK CAPITALS as the last line of an address. Do not underline the postcode or use any punctuation. Leave a clear space of one character between the two parts of the postcode and do not join the characters in any way.

How many letters are in a postcode?

The postcode is a combination of between five and seven letters/numbers, which define four different levels of geographic unit.


3 Answers

You can use this regex:

([A-Z]+)([0-9]+).*?[A-Z0-9]{3}

and use matched group #1 and #2

like image 176
anubhava Avatar answered Oct 18 '22 07:10

anubhava


this regex:

([A-Z]+)([0-9]+)([A-Z ]*[0-9][A-Z]{2})

works as follows according to RegexBuddy 4 simulation of MySQL's POSIX ERE regex implementation:

Input     G1   G2   G3
--------  ---  ---  ------
AA9A 9AA  AA   9    A 9AA
A9A 9AA   A    9    A 9AA
A9 9AA    A    9     9AA
A99 9AA   A    99    9AA
AA9 9AA   AA   9     9AA
AA99 9AA  AA   99    9AA
AA9A9AA   AA   9    A9AA
A9A9AA    A    9    A9AA
A99AA     A    9    9AA
A999AA    A    99   9AA
AA99AA    AA   9    9AA
AA999AA   AA   99   9AA

explanation:

([A-Z]+)    # captures one or more letters
([0-9]+)    # captures one or more digits
(           # starts 3rd capture group
 [A-Z ]*    # captures zero or more letters or space
 [0-9]      # captures exactly one digit
 [A-Z]{2}   # captures exactly two letters
)           # ends 3rd capture group

let me know if I missed a test case that doesn't work, and I'll see what I can do

like image 39
Code Jockey Avatar answered Oct 18 '22 07:10

Code Jockey


"[A-Z](?:\\D\\d\\D|\\d\\D|\\d|\\d{2}|\\D\\d|\\D\\d\\d|EI)\\s??\\d{1}\\D{2}" works quite well for me. It is littlebit complicated because I use it to fetch zipcode from ONE LINE ADDRESS (whole address with city, country, street names etc, and not for validating zipcodes). I use it in my production environment.

like image 1
Antoniossss Avatar answered Oct 18 '22 06:10

Antoniossss