RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find Pattern Title
Expression
(((^[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] ) ) (\s*)? ([0-9][ABD-HJLNP-UW-Z]{2})) | (^GIR\s?0AA)
Description
Here it is, the monster UK Postcode regular expression that should prevent the majority of miskeying. Unlike an most other regexes it only permits valid combinations of letters, e.g. BN works, BM does not. Spaces between the first and second parts are optional. I have tested this against a file of 7,500 valid postcodes, and also against a real-world list of about 12,000 which had some bad data. Manually checking a sample of the rejected ones showed it was working correctly (and how poor the data was!!). I originally started by trying to adapt the GDSC document at http://www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm and specifically their XSD pattern at http://www.govtalk.gov.uk/gdsc/schemaHtml/bs7666-v2-0-xsd-PostCodeType.htm but this turned out to be rather loose. Instead I wrote this. If you find any valid postcodes being rejected by this regex please let me know.
Matches
W1A 1AA | EC2V 1JN | GIR 0AA
Non-Matches
TB12 1AB | EC2V 1JM | W2A 1AA
Author Rating: The rating for this expression. Howard Richards
Source
Your Rating
Bad Good

Enter New Comment

Title
 
Name
 
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: SW1A 0AA
Name: James
Date: 10/17/2011 4:56:32 PM
Comment:
seems to be rejecting the following valid london postcodes: SW1A 0AA SW1A 0PW SW1A 1AA SW1A 2AA SW1A 2AB SW1A 2HQ your other script accepts them


Title: Amended version
Name: John Donoghue
Date: 10/23/2008 5:52:56 AM
Comment:
Nice work. I've slightly amended it to also match E1 and W1 outcodes, plus British Forces and overseas dependent territories. ^((([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)|E1|W1)[ABEHMNPRVWXY]))(\s*)?([0-9][ABD-HJLNP-UW-Z]{2}))$|^(GIR\s?0AA)$|^(ASCN|BBND|BIQQ|FIQQ|PCRN|SIQQ|STHL|TDCU|TKCA)\s?1ZZ$|^(BFPO\s?\d\d?\d?)$


Title: E1W
Name: Nick English
Date: 10/10/2008 9:12:25 AM
Comment:
The regex is rejecting E1W postcodes which seem to be valid.


Title: problem
Name: jayant
Date: 7/28/2008 6:09:05 AM
Comment:
hi, i used this expression in VS 2005, and no matter what postcode i enter, its giving me error.


Title: problem
Name: jayant
Date: 7/28/2008 6:06:51 AM
Comment:
hi, i used this expression in VS 2005, and no matter what postcode i enter, its giving me error.


Title: Amendment to anchor at end
Name: Vinay
Date: 5/25/2007 12:22:26 PM
Comment:
The following is an adaptation of yours, with whitespace removed and ^,$ anchors added. ^((([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]))(\s*)?([0-9][ABD-HJLNP-UW-Z]{2}))$|^(GIR\s?0AA)$


Title: OX18 3PDabc seems to pass, when it should not
Name: Vinay
Date: 5/25/2007 12:08:56 PM
Comment:
As per title. Your regex does not terminate with a $, so there can be stuff following the postcode, which should IMO be trapped.


Title: Thank you (and testing notes)
Name: Andrew Sinclair
Date: 5/9/2007 9:17:13 AM
Comment:
Thanks for this Howard. I'm using this with ASP.NET 2.0 and a RegularExpressionValidator. If the validation is run client side then there seems to be an issue because Javascript has slightly different regex syntax to .NET. I found that changing [0-9][ABD-HJLNP-UW-Z]{2} to [0-9][ABD-HJLNP-UW-Z][ABD-HJLNP-UW-Z] did the trick. As mentioned elsewhere, another gotcha is to ensure that pattern whitespace is ignored (in fact for RegularExpressionValidator you can't easily switch this on so it's best to strip out the whitespace from the pattern altogether.)


Title: Mr
Name: Bond
Date: 4/13/2007 6:47:31 AM
Comment:
Anyone know how to modify this reg ex to accept postcodes such as E14P 7GT?


Title: Thanks
Name: Alex
Date: 2/19/2007 10:32:54 AM
Comment:
Just wanted to say thanks. This works great. Only thing to note (if you didn't get it from reading the pattern!!) is that characters must be uppercase. I've tested this with .NET 1.1 & .NET 2.0 using C# and VB. :o)


Title: just perfect
Name: mtm
Date: 11/9/2006 7:38:10 AM
Comment:
Works perfect with 200,000 unique UK postcodes, found 500 invalid which was expected. "Dont waste your time with this rubbish". Oh yes? In Perl just use the 'x' flag ( m/^regex$/x ) and it works fine.


Title: Problems with
Name: Peter Bond Regular Expressions in Visual Studio
Date: 2/24/2006 12:31:30 PM
Comment:
I can't get this to accept any UK postcodes at all. I am using Visual Studio, should that cause a problem?


Title: Note to anyone using / testing
Name: Howard
Date: 4/15/2005 7:30:11 AM
Comment:
PLEASE NOTE: if you wish to test or use this patterm you should tick IGNORE WHITESPACE (or remove the spaces from the pattern manually). I left them in to help anyone trying to examine or debug the pattern.


Title: How funny
Name: Howard
Date: 4/15/2005 7:08:55 AM
Comment:
Well I managed to debug your expression by removing the first {1,2} - not sure how you can combine ? with {1,2} so I'm not surprised that Expresso rejected it. I'd love to know why you were rude about my contribution, which I have tested, when you then posted an alternative when it's so totally, laughably useless.. here are the two main problems I can see: 1. Since when did UK postcodes permit a numeric digit in the first character? 2. It permits a fantastic array of invalid postcodes such as A999 0AA, A1AA 0AA. Although no postcode regex can hope to validate to a real postcode, it should at least try to validate the structure and that is what mine is supposed to do.


Title: Thanks
Name: Howard
Date: 4/15/2005 6:53:20 AM
Comment:
Thank you for your 'polite' comment Kevin - very revealing about you, if not very helpful. I hope you didn't use regexlib to test because I've not found it reliable. I built and tested this on Expresso and it is in use on an ASP.NET application. As I stated it has been tested with something like 19,000 postcodes with both valid and invalid data. It DOES work with the examples in expresso and in .NET The alternative you posted has an error in it so I can't comment on whether it works or not.


Title: Doesn't work
Name: Kevin Greensmart
Date: 4/15/2005 6:02:33 AM
Comment:
Doesn't even work with the examples given. Dont waste your time with this rubbish. Use: ^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$ instead


Copyright © 2001-2024, RegexAdvice.com | ASP.NET Tutorials