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-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$
Description
Regular expression to match valid UK postcodes. In the UK postal system not all letters are used in all positions (the same with vehicle registration plates) and there are various rules to govern this. This regex takes into account those rules. Details of the rules: First half of postcode Valid formats [A-Z][A-Z][0-9][A-Z] [A-Z][A-Z][0-9][0-9] [A-Z][0-9][0-9] [A-Z][A-Z][0-9] [A-Z][A-Z][A-Z] [A-Z][0-9][A-Z] [A-Z][0-9] Exceptions Position - First. Contraint - QVX not used Position - Second. Contraint - IJZ not used except in GIR 0AA Position - Third. Constraint - AEHMNPRTVXY only used Position - Forth. Contraint - ABEHMNPRVWXY Second half of postcode Valid formats [0-9][A-Z][A-Z] Exceptions Position - Second and Third. Contraint - CIKMOV not used
Matches
DN3 6GB | SW42 4RG | GIR 0AA
Non-Matches
SEW4 5TY | AA2C 4FG | AA2 4CV
Author Rating: The rating for this expression. Stuart Wade
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: Post code match from anywhere in text/string
Name: Faisal Khan
Date: 2/29/2012 6:02:59 PM
Comment:
this is exactly same as Trev did but this would match post codes from within the block of text like UB24SF London or uxbridge road UB24SF , etc. This would work as long as post code is a separate word i.e. UB2 4SF-London will not match \b([A-PR-UWYZa-pr-uwyz]([0-9]{1,2}|([A-HK-Ya-hk-y][0-9]|[A-HK-Ya-hk-y][0-9]([0-9]|[ABEHMNPRV-Yabehmnprv-y]))|[0-9][A-HJKS-UWa-hjks-uw])\ {0,1}[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}|([Gg][Ii][Rr]\ 0[Aa][Aa])|([Ss][Aa][Nn]\ {0,1}[Tt][Aa]1)|([Bb][Ff][Pp][Oo]\ {0,1}([Cc]\/[Oo]\ )?[0-9]{1,4})|(([Aa][Ss][Cc][Nn]|[Bb][Bb][Nn][Dd]|[BFSbfs][Ii][Qq][Qq]|[Pp][Cc][Rr][Nn]|[Ss][Tt][Hh][Ll]|[Tt][Dd][Cc][Uu]|[Tt][Kk][Cc][Aa])\ {0,1}1[Zz][Zz]))\b


Title: Uk Post code
Name: ibrar
Date: 6/15/2011 7:52:52 AM
Comment:
try this ^(((([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))$ is more reliable


Title: Uk Post code
Name: ibrar
Date: 6/15/2011 7:37:55 AM
Comment:
try this ^(((([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))$ is more reliable


Title: Allow a space at the end.
Name: Terry
Date: 5/13/2011 9:10:27 AM
Comment:
Would like to modify this to allow for a space at the end of the postcode (as opposed to the middel). I have received complaints from people saying they can not enter their postcode. I assume it might be because they copy and paste the postcode in and often then adds a space at the end of the postcode which I have tested and realise is not accepted.


Title: Do you trust Wikipedia?
Name: Mikloa
Date: 1/25/2011 5:23:40 AM
Comment:
I got this from Wikipedia: (GIR 0AA)|(((A[BL]|B[ABDHLNRSTX]?|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[AEGKLMNOPRSTY]?|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)[1-9]?[0-9]|((E|N|NW|SE|SW|W)1|EC[1-4]|WC[12])[A-HJKMNPR-Y]|(SW|W)([2-9]|[1-9][0-9])|EC[1-9][0-9]) [0-9][ABD-HJLNP-UW-Z]{2}) Apparently it excludes all invalid areas and some invalid districts (of course these might change with time), and also matches the new non-geographic BX codes. Haven't tested it fully yet so use with caution!


Title: Issue with Expression regarding spaces.
Name: Dave
Date: 10/21/2010 3:59:03 PM
Comment:
The RegEx works excellent, and I am very pleased (being a RegEx novice). How do I modify the RegEx to not allow two spaces in between the characters? It doesn't allow three, but it allows 1 and 2.


Title: How about a simpler solution?
Name: Gaby Tredl
Date: 3/12/2010 3:43:40 AM
Comment:
([A-Z][A-Z]?[0-9][0-9]?[A-Z]? [0-9]?[A-Z]?[A-Z]? ?) Or is it too simple? Seems to work for me...


Title: How about a simpler solution?
Name: Gaby Tredl
Date: 3/12/2010 3:43:00 AM
Comment:
([A-Z][A-Z]?[0-9][0-9]?[A-Z]? [0-9]?[A-Z]?[A-Z]? ?) Or is it too simple? Seems to work for me...


Title: how to add hyphen and underscore in the UK postcode regex
Name: nbsamurai
Date: 1/12/2010 4:28:54 AM
Comment:
I'm using ^([A-PR-UWYZa-pr-uwyz]([0-9]{1,2}|([A-HK-Ya-hk-y][0-9]|[A-HK-Ya-hk-y][0-9]([0-9]|[ABEHMNPRV-Yabehmnprv-y]))|[0-9][A-HJKS-UWa-hjks-uw])\ {0,1}[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}|([Gg][Ii][Rr]\ 0[Aa][Aa])|([Ss][Aa][Nn]\ {0,1}[Tt][Aa]1)|([Bb][Ff][Pp][Oo]\ {0,1}([Cc]\/[Oo]\ )?[0-9]{1,4})|(([Aa][Ss][Cc][Nn]|[Bb][Bb][Nn][Dd]|[BFSbfs][Ii][Qq][Qq]|[Pp][Cc][Rr][Nn]|[Ss][Tt][Hh][Ll]|[Tt][Dd][Cc][Uu]|[Tt][Kk][Cc][Aa])\ {0,1}1[Zz][Zz]))$ for UK Postcode. But how to include '-' and '_' characters along with the post codes instead of white spaces ?


Title: From the UK Government.
Name: Nick Hanley
Date: 2/24/2009 1:03:05 PM
Comment:
According to the UK Government, on this page... http://www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm The correct matching string for a UK address is; (GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[IJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2})


Title: Try this one...
Name: Trev
Date: 2/6/2009 8:13:19 PM
Comment:
I found this one here: http://snipplr.com/view/7990/validate-uk-postcode/ I've added lower case characters and made spaces optional. Probably not the most efficient but it seems to work with all the problem ones in this thread. ^([A-PR-UWYZa-pr-uwyz]([0-9]{1,2}|([A-HK-Ya-hk-y][0-9]|[A-HK-Ya-hk-y][0-9]([0-9]|[ABEHMNPRV-Yabehmnprv-y]))|[0-9][A-HJKS-UWa-hjks-uw])\ {0,1}[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}|([Gg][Ii][Rr]\ 0[Aa][Aa])|([Ss][Aa][Nn]\ {0,1}[Tt][Aa]1)|([Bb][Ff][Pp][Oo]\ {0,1}([Cc]\/[Oo]\ )?[0-9]{1,4})|(([Aa][Ss][Cc][Nn]|[Bb][Bb][Nn][Dd]|[BFSbfs][Ii][Qq][Qq]|[Pp][Cc][Rr][Nn]|[Ss][Tt][Hh][Ll]|[Tt][Dd][Cc][Uu]|[Tt][Kk][Cc][Aa])\ {0,1}1[Zz][Zz]))$


Title: Pretty good, but...
Name: Premasagar
Date: 11/20/2008 7:51:16 PM
Comment:
This incorrectly matches "1G 8LZ", but not the correct "W1G 8LZ".


Title: Expression does not work in .Net validation control
Name: TonyMcC
Date: 10/2/2008 6:50:31 AM
Comment:
Hi, This expression does not actually work when using the Validation control in .Net!


Title: THANKS MATT
Name: jay
Date: 9/4/2008 5:55:35 AM
Comment:
ur expression very fine. nice job.


Title: Reg Ex that takes both lower and upper case and checks for space.
Name: Matt
Date: 5/27/2008 7:49:21 AM
Comment:
here is an expression that can take both upper and lower case, and a space inbetween the post code. Hope this helps. ^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) {0,1}[0-9][A-Za-z]{2})$


Title: This might help a little
Name: Joseph
Date: 4/15/2008 6:26:06 PM
Comment:
Hi all Nice debate, but can be somewhat unecessary if you don't mind me saying so. If you look here http://www.evoxfacilities.co.uk/evoxar.html and here http://www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm, it should help ease your problems. I too went through all that hassle, but I think it's a lot easier to put information about how postcodes are contructed into a dB and then check against known values, isn't it?


Title: This might help a little
Name: Joseph
Date: 4/15/2008 6:25:16 PM
Comment:
Hi all Nice debate, but can be somewhat unecessary if you don't mind me saying so. If you look here http://www.evoxfacilities.co.uk/evoxar.html and here http://www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm, it should help ease your problems. I too went through all that hassle, but I think it's a lot easier to put information about how postcodes are contructed into a dB and then check against known values, isn't it?


Title: Matching incorrect postcodes
Name: Campbell Ritchie
Date: 1/3/2008 12:33:27 PM
Comment:
Not convinced. This regex will match SW1A 0AA which is real, and also SWAA 0AA which isn't real.


Title: The current Wikipedia one is inefficient
Name: Mark
Date: 9/27/2007 12:15:29 PM
Comment:
It causes uneccessary backtracking... a better expression is: ^(GIR 0AA)|([A-PR-UWYZ]((\d(\d|[A-HJKSTUW])?)|([A-HK-Y]\d(\d|[ABEHMNPRV-Y])?)) \d[ABD-HJLNP-UW-Z]{2})$ This acheives the same level of accuracy (it still ignores BFPO addresses) but doesn't cause as much backtracking.


Title: This one seems to work well.
Name: Wayne Young
Date: 8/5/2007 6:13:31 AM
Comment:
I found this on Wikipedia. It seems to work with W1G 9XT, and other postcodes mentioned here that dont work withe posted expression. (GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW]) [0-9][ABD-HJLNP-UW-Z]{2})


Title: This one seems to work well.
Name: Wayne Young
Date: 8/5/2007 6:13:21 AM
Comment:
I found this on Wikipedia. It seems to work with W1G 9XT, and other postcodes mentioned here that dont work withe posted expression. (GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW]) [0-9][ABD-HJLNP-UW-Z]{2})


Title: Corrected
Name: Michael
Date: 6/12/2007 7:55:33 AM
Comment:
"/([A-PR-UWYZ](([A-HK-Y]{1}[0-9]{1}[ABEHMNPRVWXY0-9]{0,1})|([0-9]{1}[A-HJKSTUW0-9]{0,1})) [0-9]{1}[ABD-HJLNP-UW-Z]{2})|GIR 0AA/"; This is, as far as I am aware from the available documentation, correct - unlike the ones here. All the regex's here seem to allow WW 3EG as okay. It's not, IF there is a letter in the second position, it MUST be followed by a number, and optionally a letter/number in the fourth position as well. Feel free to check this, and correct in the event I have missed something (or simplify instead if I've used excess coding).


Title: mmm
Name: vvv
Date: 6/5/2007 9:59:41 AM
Comment:
THE ABOVE IS NOT QUITE THERE. THIS ONE WORKS FOR US: ^([A-PR-UWYZ][A-HK-Y0-9][A-HJKS-UW0-9]?[ABEHMNPRVWXY0-9]?{1,2} [0-9][ABD-HJLNP-UW-Z]{2})$


Title: Even the Government don't know
Name: Matt Sherar
Date: 5/23/2007 4:56:28 AM
Comment:
I followed the link found below http://www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm the text isn't exactly easy to understand. But there is a link to a schema definition. Great I thought I can just copy that. But the schema documentation does not even match the restriction pattern!!!! The pattern they (the british government) actually use is "[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z-[CIKMOV]]{2}"


Title: This regex needs revising
Name: John
Date: 4/12/2007 9:23:22 AM
Comment:
We use these rules (not as a regex) in our existing postcode validation. The rules are (now) incorrect, hence me googling for a regex. This one is broken, because it implements those same old rules. How do we replace it with the correct one?


Title: This regex needs revising
Name: John
Date: 4/12/2007 5:55:17 AM
Comment:
We use these rules (not as a regex) in our existing postcode validation. The rules are (now) incorrect, hence me googling for a regex. This one is broken, because it implements those same old rules. How do we replace it with the correct one?


Title: Doesn't cover all UK postcodes any more
Name: James West-Sadler
Date: 1/17/2007 4:52:25 AM
Comment:
Unfortunately this fails on W1G 9XT - which is a valid UK postcode


Title: Non match for valid postcode
Name: Foamcow
Date: 9/28/2006 5:19:11 AM
Comment:
The non matching text indicates that the postcode SW1A 1AA would be invalid, whereas it is a valid UK postcode.


Title: BUG FIX
Name: SHUNT
Date: 9/26/2006 11:50:41 AM
Comment:
THE ABOVE IS NOT QUITE THERE. THIS ONE WORKS FOR US: ^([A-PR-UWYZ][A-HK-Y0-9][A-HJKS-UW0-9]?[ABEHMNPRVWXY0-9]?{1,2} [0-9][ABD-HJLNP-UW-Z]{2})$


Title: Conversion of mask for Microsoft SQL
Name: Daniel Watkins
Date: 12/8/2005 2:08:31 AM
Comment:
I can get this expression to work with Microsoft SQL 2000? How can I convert it to check postcodes in a rule? I've had a go with no success! Email me at danielwatkinslearn@hotmail.com if you have any suggestions!


Title: Expression not accepting a valid post code
Name: Swet Batra
Date: 10/10/2005 4:11:35 AM
Comment:
Hi there I am trying to use a valid post code (W1K 5DL) but the expression specified above is not accepting this as a valid post code. I had reffered to the uk govt. site i.e. http://www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm. As per this my post code is correct. As a correction i feel the reg ex specified above should be replaced with ^([A-PR-UWYZ0-9][A-HK-Y0-9][ABCDEFGHJKSTUW0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLNP-UW-Z]{2}|GIR 0AA)$ I may be wrong but please let me know the results. Thanks and Regards Swet Batra swbatra@sapient.com


Title: Correction
Name: Craig McDowall
Date: 7/13/2005 10:07:12 AM
Comment:
It should actually be: /(^gir\s0aa$)|(^[a-pr-uwyz]((\d{1,2})|([a-hk-y]\d{1,2})|(\d[a-hjks-uw])|([a-hk-y]\d[abehmnprv-y]))\s\d[abd-hjlnp-uw-z]{2}$)/i


Title: A new one
Name: Craig McDowall
Date: 7/13/2005 10:01:20 AM
Comment:
Here is one that works using the spec from the government web site: /(^gir\s0aa$)|(^[a-pr-uwyz]((\d{1,2})|([a-hk-y]\d{1,2})|(\d[a-hjks-uw])|([a-hk-y]\d[abehmnprv-y]))\s\d[abd-hjlnp-ux-z]{2}$)/i


Title: would be better if accepted lower case
Name: rw
Date: 5/12/2005 6:13:03 AM
Comment:
would be better if accepted lower case


Title: Problems
Name: Stuart Wade
Date: 5/7/2005 6:34:01 PM
Comment:
All these comments, and the website won't let me alter the regex! As for the last one, the regex was designed to only allow regexes which were correctly formated to work.


Title: Correct Example
Name: Mike
Date: 5/7/2005 6:01:48 PM
Comment:
Here is a more simplified example with all the errors pointed out corrected. ^(([A-PR-UWYZ]{1,2}[0-9]{1,2}[ABEHJMNPRVWXY]?)\s?([0-9][ABD-HJLNP-UW-Z]{2})|(GIR)\s?(0AA))$ It seems like the original regex would validate postcodes like 7777 5TG, but not postcodes without spaces like N45RT which people often enter. It also places the 2 parts of the postcode into $1 and $2 so you can enter the space for correctness in the database


Title: Mistake in CIKMOV exclusion
Name: Dale Robinson
Date: 5/28/2004 10:05:11 AM
Comment:
Hello, I think there is is mistake, according to its own description. The chracter range to exclude CIKMOV should be: [ABD-HJLNP-UW-Z], note the P after the N, not the one shown, as it doesn't seem to exclude O. Dale Robinson


Title: some valid postcodes not recognised
Name: dave
Date: 5/13/2004 7:36:59 AM
Comment:
W1J 8DP is valid uk postcode, but not recognised.


Title: Does not work with new UK postcodes
Name: Kim
Date: 5/13/2004 7:01:04 AM
Comment:
Does not work with new UK postcodes


Title: Does not work with new UK postcodes
Name: JM
Date: 5/13/2004 7:00:24 AM
Comment:
Does not work with new UK postcodes, use http://www.regexlib.com/REDetails.aspx?regexp_id=695 instead.


Title: .NET Test exception
Name: Peter
Date: 4/19/2004 7:58:45 AM
Comment:
the above seems to cause an exception when used with .NET Test Button. Substitute ?[ ]{1,2} for the space checking contraint seems to solve this


Title: Problems with this expression
Name: David Wheeler
Date: 3/25/2004 2:01:43 PM
Comment:
This is a decent effort, apart from the fact that this expression matches 11 1AA as a valid postcode, and W1AA as a valid outcode (first half of the postcode).


Title: Broken Even bigger expression
Name: Nigel Metheringham
Date: 8/28/2003 5:47:02 AM
Comment:
The comment handling system truncated the posted expression. It ends (starting from intermediate space):- \s* # random spacing in between (\d # first character of inward - digit [ABD-HJLN-UW-Z]{2}) # 2nd or 3rd character - limited alphas /x


Title: Even bigger expression
Name: Nigel Metheringham
Date: 8/28/2003 5:41:10 AM
Comment:
Merged the couple of expressions I found on the web, to come up with this monster (in perl /x format - ie lots of extra spacing) - BTW I capture the first and second parts separately and allow arbitary spacing between the parts, so the final cleaned up postcode is "$1 $2" /((?:A[BL]| # Outward - first one or two letters B[ABDHLNRST]?| # only certain combinations used C[ABFHMORTVW]| D[ADEGHLNTY]| E[CHNX]?| F[KY]| G[LUY]?| H[ADGPRSUX]| I[GMPV]| JE| K[ATWY]| L[ADELNSU]?| M[EKL]?| N[EGNPRW]?| O[LX]| P[AEHLOR]| R[GHM]| S[AEGKLMNOPRSTWY]?| T[ADFNQRSW]| UB| W[ACDFNRSV]?| YO| ZE) \d # 2nd or 3rd character - digit (?:\d|[A-Z])? # final character of outward - digit or letter ) # end of outward half \s* # random spacing in between (\d # first character of inward - digit [ABD-HJLN-UW-Z]{2}) # 2nd or 3rd charact


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