in reply to UK postcode regex

You don't need to use regex for this either:
substr($code, -3, 0) = ' ' if substr($code, -4, 1) ne ' '; substr($code, -3, 1) = '0' if substr($code, -3, 1) eq 'O'; $code = uc($code);