The following is the UK Postcode Regular Expression and the corresponding detail explaining the logic behind the UK Postcode Regular Expression.
^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$
"GIR 0AA"
OR
One letter followed by either one or two numbers
OR
One letter followed by a second letter that must be one of ABCDEFGHJKLMNOPQRSTUVWXY(i.e..not I) and then followed by either one or two numbers
OR
One letter followed by one number and then another letter
OR
A two part post codewhere the first part must be
One letter followed by a second letter that must be one of ABCDEFGHJKLMNOPQRSTUVWXY (i.e..not I) and then followed by one number and optionally a further letter after that
AND
The second part (separated by a space from the first part) must be One number followed by two letters.
A combination of upper and lower case characters is allowed.
Note: the length is determined by the regular expression and is between 2 and 8 characters
This is extracted from https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/488478/Bulk_Data_Transfer_-_additional_validation_valid_from_12_November_2015.pdf