Better version of UK postcode

Published on Tue Mar 01 2022

This UK postcode regex will match all variants of postcode formats including inner London postcodes (eg SW1A), with optional space (both AL11XP and AL1 1XP are valid) It will reject any postcode that does not specify a valid postcode area. Each part of the postcode is also captured into a capture group as follows: group 1: POSTCODE AREA group 2: POSTCODE DISTRICT group 3: POSTCODE SECTOR group 4: POSTCODE UNIT