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

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find Pattern Title
Expression
^((([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])))|(((GI)(R))\s{0,2}((0)(A)(A)))$
Description
Validates UK postcodes and aligns in parenthesized subpatterns according to standard UK postcode elements. Validation is according to guidelines outlined at http://www.govtalk.gov.uk/gdsc/html/noframes/PostCode-2-1-Release.htm Postcode elements are separated according to description at http://www.graticule.com/data/uk/pcdata.html Expression is not thoroughly tested, but seems to be doing ok so far! Comments and improvements welcome.
Matches
GIR 0AA | SW1Y 1AA | AB1 4BL
Non-Matches
XVJ P09 | GB7Z 1HQ | Q1 1AA
Author Rating: Not yet rated. Christopher Townson
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: New Revision of above regex
Name: Lee
Date: 5/1/2007 6:20:31 AM
Comment:
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
Comment:
gov talk have provided a regex of their own here: http://www.govtalk.gov.uk/gdsc/schemaHtml/bs7666-v2-0-xsd-PostCodeType.htm


Title: Pedro, try this
Name: Howard
Date: 4/16/2005 6:36:54 AM
Comment:
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
Comment:
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
Comment:
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
Comment:
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
Comment:
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
Comment:
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
Comment:
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: http://www.govtalk.gov.uk/gdsc/html/noframes/PostCode-2-1-Release.htm


Title: some unexpected problems
Name: Emma
Date: 10/7/2004 11:26:35 AM
Comment:
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
Comment:
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-2018, RegexAdvice.com | ASP.NET Tutorials