RegEx for matching UK Postcodes

前端 未结 30 2446
广开言路
广开言路 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 01:54

    To add to this list a more practical regex that I use that allows the user to enter an empty string is:

    ^$|^(([gG][iI][rR] {0,}0[aA]{2})|((([a-pr-uwyzA-PR-UWYZ][a-hk-yA-HK-Y]?[0-9][0-9]?)|(([a-pr-uwyzA-PR-UWYZ][0-9][a-hjkstuwA-HJKSTUW])|([a-pr-uwyzA-PR-UWYZ][a-hk-yA-HK-Y][0-9][abehmnprv-yABEHMNPRV-Y]))) {0,1}[0-9][abd-hjlnp-uw-zABD-HJLNP-UW-Z]{2}))$
    

    This regex allows capital and lower case letters with an optional space in between

    From a software developers point of view this regex is useful for software where an address may be optional. For example if a user did not want to supply their address details

提交回复
热议问题