Regular Expression for England only Postcode

I have an Asp.Net website and I want to use the RegularExpressionValidator to check if the UK zip code is English (i.e. not Scottish, Welsh or N.Irish).

It should be possible to find out if the zip code is English using only the letters from the first separator (called the zip code area). There are 124 postal codes in total, and this is a list of them.

From this list, the following zip code areas are not located in England.

  • ZE, KW, IV, HS, PH, AB, DD, PA, FK, G, KY, KA, DG, TD, EH, ML (Scotland)
  • LL, SY, LD, HR, NP, CF, SA (Wales)
  • BT (N.Ireland)

The regex input can be the entire zip code, or it can only be a zip code area.

Can someone help me create a regex that only matches if the given zip code is English?

EDIT - solution

With a few posters, I was able to create the following regular expression, which I tested with over 1,500 test tests.

^ (AL | B | B [ABDHLNRS] | C [ABHMORTVW] | D [AEHLNTY] | E | E [CNX] | FG | G [Luy] | H [ADGPUX] | I [GM P] | JE | CT | L | L [AENSU] | M | M | N | N [EGNRW] | O [LX] | P [ELOR] | P [GCM] | S | S [EGKLMNOPRSTW] | T [AFNQ RSW] | UB | W | W [ACDFNRSV] |? YO) \ d {1,2} \ s (\ d [\ w] {2})

+5
source share
6 answers

There are 124 postal codes in the UK.

- PAF®   2012, ().

( ):

  • ?

    Postcode

  • ?

    :

    • :
      • ! /^(ZE|KW|IV|HS|PH|AB|DD|PA|FK|G|KY|KA|DG|TD|EH|ML)[0-9]/
    • :
      • ! /^(LL|SY|LD|HR|NP|CF|SA)[0-9]/
    • , , ,...
      • et cetera...
    • , , , ☻

, . .

+4

, , 100% ( ).

, ... , , .

, - . :

  • BT ( )
  • IM ( )
  • JE ()
  • GG ()
  • BF ( )
  • BX ( )
  • GIR (Girobank, )

( , -, .. , Royal Mail .)

( ):

ZE, KW, IV, HS, PH, AB, DD, PA, FK, G, KY, KA, EH, ML

DG TD . :

  • DG16 -
  • TD9 -
  • TD12 -
  • TD15 -

:

DG16 , :

  • DG16 5H [TUZ]
  • DG16 5J [AB]

TD9 , TD9 0T [JPRSTUW]

TD12 (TD12 4), :

  • TD12 4 [ABDEHJLN]
  • TD12 4 [QRSTUWX]

TD15 . 3 , TD15 2 TD15 9 .

TD15 1 .

:

  • TD15 1T
  • TD15 1X

... :

  • TD15 1T [ABQUX]
  • TD15 1XX

TD15 1 , , :

  • TD15 1B
  • TD15 1S (.. TD15 1S [ABEJLNPWXY])
  • TD15 1U (.. TD15 1U [BDENPQRTUXY])

..., , , :

  • TD15 1BT
  • TD15 1S [UZ]
  • TD15 1U [FGHJLSZ]

CA NE -, .

, , ( ), , . , , , , (.. ) , . . TD9 0TJ TD15 1UZ , , .

- , .

+7

, , , .

, CH1 , CH5 .

, , TD12 - , - .

, , BT ( )

+3

^(AB|AL|B| ... )$, ... , , (|).

: : http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom

/, - ^(AB|AL|B| ... )([\d\w]{3})\s([\d\w]{3})$, .

^(A[BL]|B[ABDHLNRST]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[CNX]?|F[KY]|G[LUY]|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTWY]?|T[AFNQRSW]|UB|W[ACDFNRSV]?|YO|ZE)([\w\d]{1,2})\s?([\w\d]{3})$

. , 1 2 {1,2} \w \d, \s?, 3 . , .

+1

RegEx i, Royal Mail :

:

/^([A-PR-UWYZ](?:[0-9]{1,2}|[0-9][A-HJKMNPR-Y]|[A-HK-Y][0-9]{1,2}|[A-HK-Y][0-9][ABEHMNPRVWXY]))\s*([0-9][ABD-HJLNP-UW-Z]{2})$/i

GiroBank:

/^(GIR)\s*(0AA)$/i

:

/^([A-Z]{4})\s*(1ZZ)$/i

:

/^(BFPO)\s*(?:(c\/o)\s*)?((?(2)[0-9]{1,3}|[0-9]{1,4}))$/i

, , :

public function UKPostCode(&$strPostCode, &$strError = null, &$strType = null, $ReturnFormatted = true) {
    $strStrippedPostCode = preg_replace("/[\s\-]/i", "", $strPostCode);

    if (empty($strStrippedPostCode)) {
        $strError = $this->__getErrorMessage("Post", "EMPTY_POST");
        return false;

    }

    $arrRegExp = array(
        "STD" => "/^([A-PR-UWYZ](?:[0-9]{1,2}|[0-9][A-HJKMNPR-Y]|[A-HK-Y][0-9]{1,2}|[A-HK-Y][0-9][ABEHMNPRVWXY]))\s*([0-9][ABD-HJLNP-UW-Z]{2})$/i",
        "GIR" => "/^(GIR)\s*(0AA)$/i",
        "OST" => "/^([A-Z]{4})\s*(1ZZ)$/i",
        "BFPO" => "/^(BFPO)\s*(?:(c\/o)\s*)?((?(2)[0-9]{1,3}|[0-9]{1,4}))$/i"
    );

    foreach ($arrRegExp as $strPostCodeType => $strExpression) {

        if (preg_match($strExpression, $strPostCode, $arrMatches)) {

            if ($ReturnFormatted !== null) {
                array_shift($arrMatches);
                $strPostCode = implode(" ", array_filter($arrMatches));
                $strPostCode = ((bool)$ReturnFormatted === true) ? strtoupper($strPostCode) : strtolower($strPostCode);

            }

            $strType = $strPostCodeType;
            return true;

        }

    }

    $strError = $this->__getErrorMessage("Post", "INVALID_POST");
    return false;

}

,

+1
'A[BL]|B[ABDHLNRST]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[CNX]?|F[KY]|G[LUY]|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTWY]?|T[AFNQRSW]|UB|W[ACDFNRSV]?|YO|ZE'
0

All Articles