First of all:
UK postal code correct regex is (more or less seems to be similar to your, but I have no time/will to check every possible case):
^(([A-Z]\d{2}[A-Z]{2})|([A-Z]\d{3}[A-Z]{2})|([A-Z]{2}\d{2}[A-Z]{2})|([A-Z]{2}\d{3}[A-Z]{2})|([A-Z]\d[A-Z]\d[A-Z]{2})|([A-Z]{2}\d[A-Z]\d[A-Z]{2})|(GIR0AA))$
According to the geonames' data (Official iso code of UK is GB, UK is reserved, geocode id: 2635167)
Always according to this data the format is:
@# #@@|@## #@@|@@# #@@|@@## #@@|@#@ #@@|@@#@ #@@|GIR0AA
Where, IIRC, the @
stands for character and the #
for numbers.
So to retrieve only the first part of the GB's postal codes your format will become:
@#|@##|@@#|@@##|@#@|@@#@|GIR
Assuming that this is the partial check you want to achieve the regex will become:
^(([A-Z]\d)|([A-Z]\d{2})|([A-Z]{2}\d)|([A-Z]{2}\d{2})|([A-Z]\d[A-Z])|([A-Z]{2}\d[A-Z])|(GIR))$
Of course you may want to support both uppercase and lower case characters, if this is the case, just change all the [A-Z]
occurrences to [A-Za-z]
and GIR
to [gG][iI][rR]
Remember that this will validate just the format of your postal codes not if the code exist, to do so you can download the information from geonames.org and parse them into a database for a later check.