$re = '/\b([A-PR-UWYZ](?:(?:\d{1,2}|\d[A-HJ-KSTUW])|(?:[A-HK-Y]\d(?:\d|[A-Z])?)))\s?(\d[ABD-HJLNP-UW-Z]{2})\b/';
$str = 'DD81UN
DD8 1UN
DN551PT
DN55 1PT
AN NAA M1 1AA
ANN NAA M60 1NW
AAN NAA CR2 6XH
AANN NAA DN55 1PT
ANA NAA W1A 1HQ
AANA NAA EC1A 1BB
DD8 2CA
DD8 2AA
DD8 2IA
DD8 2KA
DD8 2MA
DD8 2OA
DD8 2VA
DD8 2aa
AN
ANN
ANA
AAN
AANN
AANA
';
$subst = "$1 $2";
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php