1

I have regular expression I need to use this regular expression to validate the UK postcode. I have converted that to a format to work with asp.net regular expression engine. Could anyone please guide me what I have done is correct or if there is any mistake in it. Any help will be appreciated.These postcodes returns as validate postcodes W1A 0AX,SO10 9AA, FY9 9AA, WC1A 9AA.

Veribose Format

(GIR\s0AA) |
(
    # A9 or A99 prefix
    ( ([A-PR-UWYZ][0-9][0-9]?) |
         # AA99 prefix with some excluded areas
        (([A-PR-UWYZ][A-HK-Y][0-9](?<!(BR|FY|HA|HD|HG|HR|HS|HX|JE|LD|SM|SR|WC|WN|ZE)[0-9])[0-9]) |
         # AA9 prefix with some excluded areas
         ([A-PR-UWYZ][A-HK-Y](?<!AB|LL|SO)[0-9]) |
         # WC1A prefix
         (WC[0-9][A-Z]) |
         (
            # A9A prefix
           ([A-PR-UWYZ][0-9][A-HJKPSTUW]) |
            # AA9A prefix
           ([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY])
         )
        )
      )
      # 9AA suffix
    \s[0-9][ABD-HJLNP-UW-Z]{2}
    )

format I used to validation postcode as below

return (

    Regex.IsMatch(postcode, "([GIR[ ]0AA])") ||
    Regex.IsMatch(postcode, "(^[A-PR-UWYZ][0-9][0-9]?[ ][0-9][ABD-HJLNP-UW-Z]{2}$)") ||
    Regex.IsMatch(postcode, "(^[A-PR-UWYZ][A-HK-Y][0-9](?<!(BR|FY|HA|HD|HG|HR|HS|HX|JE|LD|SM|SR|WC|WN|ZE)[0-9])[0-9][ ][0-9][ABD-HJLNP-UW-Z]{2}$)") ||
    Regex.IsMatch(postcode, "(^[[A-PR-UWYZ][A-HK-Y](?<!AB|LL|SO)[0-9][ ][0-9][ABD-HJLNP-UW-Z]{2}$)") ||
    Regex.IsMatch(postcode, "(^WC[0-9][A-Z][ ][0-9][ABD-HJLNP-UW-Z]{2}$)") ||
    Regex.IsMatch(postcode, "(^[A-PR-UWYZ][0-9][A-HJKPSTUW][ ][0-9][ABD-HJLNP-UW-Z]{2}$)") ||
    Regex.IsMatch(postcode, "(^[A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY][ ][0-9][ABD-HJLNP-UW-Z]{2}$)")

            );
    }
aja
  • 35
  • 4
  • 1
    Is there a specific problem you're having or some scenario in which this does not work? At the moment you're essentially asking "hey guys please test and debug my code" which is off topic. – Equalsk Jan 30 '17 at 09:17
  • Please put some examples of inputs that fail into your question. How can we guess which postcodes do or do not work? – Equalsk Jan 30 '17 at 09:40
  • OK, so edit some examples into your question... The one you just provided does not appear to be a post code anyway, or at least Royal Mail can't locate it. – Equalsk Jan 30 '17 at 09:51
  • Try copy/pasting the pattern [from here](https://regex101.com/r/Ffi0el/1). – Wiktor Stribiżew Jan 30 '17 at 10:06
  • So, does `@"(GIR\s0AA)|((([A-PR-UWYZ][0-9][0-9]?)|(([A-PR-UWYZ][A-HK-Y][0-9](?<!(BR|FY|HA|HD|HG|HR|HS|HX|JE|LD|SM|SR|WC|WN|ZE)[0-9])[0-9])|([A-PR-UWYZ][A-HK-Y](?<!AB|LL|SO)[0-9])|(WC[0-9][A-Z])|(([A-PR-UWYZ][0-9][A-HJKPSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]))))\s[0-9][ABD-HJLNP-UW-Z]{2})"` work as expected? – Wiktor Stribiżew Jan 30 '17 at 10:40
  • @WiktorStribiżew thanks. I checked the pattern in both python and c# it giving the same results. – aja Feb 02 '17 at 13:34

1 Answers1

0

You may use

(GIR\s0AA)|((([A-PR-UWYZ][0-9][0-9]?)|(([A-PR-UWYZ][A-HK-Y][0-9](?<!(BR|FY|HA|HD|HG|HR|HS|HX|JE|LD|SM|SR|WC|WN|ZE)[0-9])[0-9])|([A-PR-UWYZ][A-HK-Y](?<!AB|LL|SO)[0-9])|(WC[0-9][A-Z])|(([A-PR-UWYZ][0-9][A-HJKPSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]))))\s[0-9][ABD-HJLNP-UW-Z]{2})

See the regex demo

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563