- The first Regular Expression Library on the Web!

Please support RegExLib Sponsors


Regular Expression Details

Title Test Find Pattern Title
Validates UK postcodes and aligns in parenthesized subpatterns according to standard UK postcode elements. Validation is according to guidelines outlined at Postcode elements are separated according to description at Expression is not thoroughly tested, but seems to be doing ok so far! Comments and improvements welcome.
GIR 0AA | SW1Y 1AA | AB1 4BL
XVJ P09 | GB7Z 1HQ | Q1 1AA
Author Rating: Not yet rated. Christopher Townson
Your Rating
Bad Good

Enter New Comment

Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: New Revision of above regex
Name: Lee
Date: 5/1/2007 6:20:31 AM
I found that none of the examples have worked for me (there always seems to be a valid postcode that they think is invalid) so I have rewritten it, and it seems to work fine now. it may need lower case capability added though. ^((([A-PR-UWYZ][0-9])|([A-PR-UWYZ][0-9][0-9])|([A-PR-UWYZ][A-HK-Y][0-9])|([A-PR-UWYZ][A-HK-Y][0-9][0-9])|([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]))\s?([0-9][ABD-HJLNP-UW-Z]{2})|(GIR)\s?(0AA))$

Title: GovTalk's RegExp
Name: benzo
Date: 10/30/2005 4:59:44 AM
gov talk have provided a regex of their own here:

Title: Pedro, try this
Name: Howard
Date: 4/16/2005 6:36:54 AM
Try this expression to match just the first part (((^[BEGLMNS][1-9]\d?)|(^W[2-9])|(^(A[BL]|B[ABDHLNRST]|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]|F[KY]|G[LUY]|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]|M[EKL]|N[EGNPRW]|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKL-PRSTWY]|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)\d\d?)|(^W1[A-HJKSTUW0-9])|(((^WC[1-2])|(^EC[1-4])|(^SW1))[ABEHMNPRVWXY])))

Title: Quite good
Name: Howard
Date: 4/16/2005 6:35:19 AM
A useful simple validator, but it still accepts a lot of invalid postcodes. I tried the most recent post version of this and it still accepted about 157 invalid postcodes in my test data, out of about 1423 invalid total, so it's about 90% efficient.

Title: validate first part only
Name: Pedro Costa
Date: 4/14/2005 5:47:00 AM
Hi, i'm not very experienced in regular expressions how can i split this one to only validate the first part of the post code, e.g. GIR or SW1Y? thanks.

Title: Removed More Groups
Name: Kevin Watkins
Date: 3/29/2005 8:53:50 AM
Just realised I can remove some more groups from the regex... Try: ^(?:[A-PR-UWYZ][0-9][0-9A-HJKS-UW]?|[A-PR-UWYZ][A-HK-Y][0-9][0-9ABEHMNPRV-Y]?)\s{0,2}[0-9][ABD-HJLNP-UW-Z][ABD-HJLNP-UW-Z]|GIR\s{0,2}0AA$

Title: Problems With B77 3BS
Name: Kevin Watkins
Date: 3/29/2005 8:48:22 AM
I had some problems with the valid postcode 'B77 3BS' - it would only match the B77 part. Here is a corrected regex: ^(?:[A-PR-UWYZ](?:[0-9][0-9A-HJKS-UW]?)|[A-PR-UWYZ][A-HK-Y](?:[0-9][0-9ABEHMNPRV-Y]?))\s{0,2}[0-9][ABD-HJLNP-UW-Z][ABD-HJLNP-UW-Z]|GIR\s{0,2}0AA$ I've also removed some unnecessary groups and made the other groups non-capturing to speed it up.

Title: How do you use this in JavaScript?
Name: Paolo
Date: 1/26/2005 10:04:54 AM
If I try to use this pattern in JavaScript, I get: "Invalid range charcater set"... Any ideas? Thanks, Paolo

Title: Problems resolved
Name: Jim
Date: 12/6/2004 7:32:53 AM
The problems the original expression has with N1 for example don't occur with the modified version I have quoted above which is correct to the UK Government Data Standards at:

Title: some unexpected problems
Name: Emma
Date: 10/7/2004 11:26:35 AM
This doesn't allow for some postcodes eg N1 in London which are valid.

Title: Postcode extra characters
Name: Jim
Date: 6/23/2004 8:28:53 AM
A slight tweak to stop extra characters after the postcode i.e. Q1 1AAA. Otherwise it behaves the same, hopefully. ^(((([A-PR-UWYZ][0-9][0-9A-HJKS-UW]?)|([A-PR-UWYZ][A-HK-Y][0-9][0-9ABEHMNPRV-Y]?))\s{0,2}[0-9]([ABD-HJLNP-UW-Z]{2}))|(GIR\s{0,2}0AA))$

Copyright © 2001-2022, | ASP.NET Tutorials