RegEx for matching UK Postcodes

前端 未结 30 2956
广开言路
广开言路 2020-11-22 01:38

I\'m after a regex that will validate a full complex UK postcode only within an input string. All of the uncommon postcode forms must be covered as well as the usual. For in

30条回答
  •  没有蜡笔的小新
    2020-11-22 02:01

    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 1 - QVX not used
    Position 2 - IJZ not used except in GIR 0AA
    Position 3 - AEHMNPRTVXY only used
    Position 4 - ABEHMNPRVWXY

    Second half of postcode

    • [0-9][A-Z][A-Z]

    Exceptions
    Position 2+3 - CIKMOV not used

    Remember not all possible codes are used, so this list is a necessary but not sufficent condition for a valid code. It might be easier to just match against a list of all valid codes?

提交回复
热议问题