- The first Regular Expression Library on the Web!

Please support RegExLib Sponsors


Regular Expression Details

Title Test Find Pattern Title
For the terminally hard of thinking, a version of my UK postcode validator without WHITESPACE. Unlike most validators on regexlib, this knows valid postcode combinations, e.g. W1A is permitted, W1 is not, but W2 is. It should catch between 95-98% of invalid postcodes.
W1A 1AA | EC1V1JN | N7 8BQ
W1A 1AM | BM1 1AB | EC1 1AA
Author Rating: The rating for this expression. Howard Richards
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: Post Code Validator
Name: Wyne
Date: 12/1/2011 6:13:19 AM
This result should also bring on the search result: Post Code Validator

Title: Re: Capital letters
Name: Rudi
Date: 2/8/2008 6:28:25 AM
Tada!! (untested!) (((^[BEGLMNSbeglmns][1-9]\d?)|(^[Ww][2-9])|(^([Aa][BLbl]|[Bb][ABDHLNRSTabdhlnrst]|[Cc][ABFHMORTVWabfhmort]|[Dd][ADEGHLNTYadeghlnty]|[Ee][HNXhnx]|[Ff][KYky]|[Gg][LUYluy]|[Hh][ADGPRSUXadgprsux]|[Ii][GMPVgmpv]|[Jj][Ee]|[Kk][ATWYatwy]|[Ll][ADELNSUadelnsu]|[Mm][EKLekl]|[Nn][EGNPRWegnprw]|[Oo][LXlx]|[Pp][AEHLORaehlor]|[Rr][GHMghm]|[Ss][AEGKL-PRSTWYaegkl-prstwy]|[Tt][ADFNQRSWadfnqrs]|[Uu][Bb]|[Ww][ADFNRSVadfnrsv]|[Yy][Oo]|[Zz][Ee])\d\d?)|(^[Ww]1[A-HJKSTUWa-hjkstuw0-9])|(((^[Ww][Cc][1-2])|(^[Ee][Cc][1-4])|(^[Ss][Ww]1))[ABEHMNPRVWXYabehmnprvwxy]))(\s*)?([0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}))$|(^[Gg][Ii][Rr]\s?0[Aa][Aa]$)

Title: Capital letters
Name: Sam
Date: 7/4/2007 1:48:30 PM
I'm using this on an web page - it works fine except the user has to enter capitals according to this validator. Lowercase letters are rejected, but I cant find a way to automatically capitalise the value in the clients form, nor can I rely the user to capitalise it. I can capitalise the value when its stored in the database however. Is there a way to remove capital requirement from this validator?

Title: lower case?
Name: Steve
Date: 6/1/2007 12:13:28 PM
Can we get this to work with lower case postcodes? I know that (?-i) means ignore case, but I don't know how to apply to this whole string.. Is their a simpler UK postcode checker that only checks the correct format, so that it is unlikely to break in the future?

Title: Glasgow Postcodes
Name: Chris Parkinson
Date: 5/4/2007 12:36:33 PM
Martin I have checked the patten against all 1.6 million valid Postcodes - including G40 3LH - and it works fine for me in C#. I have had to change the pattern for certain missing outbound types but that is all. "(((^[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])|(^E1[W0-9])|(((^WC[1-2])|(^EC[1-4])|(^SW1))[ABEHMNPRVWXY])|(^EC50)|(^NW1W)|(^WC99))(\s*)?([0-9][ABD-HJLNP-UW-Z]{2}))$|(^GIR\s?0AA$)"

Title: Glasgow Postcodes
Name: Martin
Date: 4/30/2007 6:53:01 AM
Hi, i have noticed a number of glasgow postcodes do not produce a valid match. For instance G40 3LH. Thanks M

Title: E14P 7GT
Name: Chris Parkinson
Date: 4/13/2007 12:25:15 PM
Mr Bond the Postcode you mention is not a valid Postcode. I have checked the newest Walksort database and it is not there.

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

Title: missing Postcodes
Name: Chris Parkinson
Date: 11/29/2006 3:49:38 PM
Howard there are four postcodes outbounds you miss E1W EC50 NW1W WC99 I have taken the newest Walksort database with 1.6 million valid Postcodes in and these ar the ones that fail all others pass through Chris Parkinson

Title: amended
Name: Howard
Date: 4/17/2005 4:27:02 PM
I've removed my warning as it now seems to work! many thanks..

Title: That's because I'm a dork
Name: Darren Neimke
Date: 4/17/2005 4:19:28 PM
I inadvertantly deleted one of the component assemblies when I was uploading the alterations. I've replaced it and run some tests which seem to indicate that things are back up and running again. Sorry about that!

Title: Another issue!
Name: Howard
Date: 4/17/2005 6:09:08 AM
I re-entered my pattern and ran a test: got the following exception report: System.IO.FileNotFoundException: File or assembly name RegexLib.Components, or one of its dependencies, was not found. File name: "RegexLib.Components" at RegExLib.Pages.UserControls.ServerResults.DisplayResults(RegexOptions opts, String pattern, String source, Boolean displayInGrid) at ASPAlliance.RegExp.Pages.RETester.btnExecuteServer_Click(Object sender, EventArgs e) === Pre-bind state information === LOG: DisplayName = RegexLib.Components, Version=1.0.1905.35320, Culture=neutral, PublicKeyToken=null (Fully-specified) LOG: Appbase = file:///d:/domains/ LOG: Initial PrivatePath = bin Calling assembly : RegExLib.Pages, Version=1.0.1905.36151, Culture=neutral, PublicKeyToken=null. === LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind). LOG: Post-policy reference: RegexLib.Components, Version=1.0.1905.35320, Culture=neutral, PublicKe

Title: OK, I've done a bit of poking around
Name: Darren Neimke
Date: 4/17/2005 4:58:17 AM
It looks as though the pattern is failing - or rather succeeding - when matching against "W1 1AA" because of the linebreaks in the pattern, specifically the linebreak between these two lines > |T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)\d\d?)|(^W1[A > -HJKSTUW0-9])|(((^WC[1-2])|(^EC[1-4])|(^SW1))[A It's most likely that this linebreak was put there due to some buggy, crappy code that I had knocked up to insert text wrapping. I've removed the crap code in question so you might try to re-save the pattern with all line breaks removed and see if it is still incorrect. Cheers, - Darren Neimke

Title: Just to save me doing this again...
Name: Darren Neimke
Date: 4/16/2005 11:26:59 PM
While reading through the pattern I took the time to expand it out with whitespace. I'll post it here just so that I don't have to do it again: --------------------------- ( ( (^[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: Fix for previous comment...
Name: Darren Neimke
Date: 4/16/2005 9:16:20 PM
I inadvertantly typed a target string of "W1 1AAA" when I should have used the same one that you had cited: "W1 1AA"

Title: Maybe they aren't bugs?
Name: Darren Neimke
Date: 4/16/2005 9:14:18 PM
Hi Howard, I'm the author of those bugs that you've been posting about today. Is it at all possible that the pattern you've shown here does in fact match against the target string "W1 1AAA"? If you doubt the regexlib testing tools that much can I suggest that you use a tool such as RegexCoach to step through your pattern and check it before posting messages that the site is inaccurate. Let me know if the tool is broken and I'll post a fix for it this afternoon. - Darren Neimke

Title: Important note
Name: Howard
Date: 4/16/2005 5:09:55 AM
VERY IMPORTANT: if testing any REGEX I suggest you **DO NOT USE REGEXLIB AS IT HAS BUGS**!! For example, in the expression above the postcode "W1 1AA" will not match in .NET (this is correct) but it WILL match on this site (which is NOT CORRECT).

Copyright © 2001-2018, | ASP.NET Tutorials