UK Postcode Regular Expression

Here is a UK postcode matching regular expression. I built it using a database of all the UK postcodes, so it does work for the strange ones.

This regular expression will match the first part:

^[A-Z]{1,2}[0-9]{1,2}[A-Z]?$

It will allow: B1, LN8, EC2M.
It will not allow: 123, 1LL, N123.

This regular expression should match the entire postcode:

^[A-Z]{1,2}[0-9]{1,2}[A-Z]? [0-9]{1,2}[A-Z]{1,3}$

It will allow: B1 7AB, LN8 12AR, EC2M 9CD.
It will not allow: 123 123, 1LL 123, N123 ASD.

Hope you find these as useful as I did.

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

Am gonna use this expresions

Thanks

Leave a comment

(required)

(required)