# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\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"
test_str = ("DD81UN\n"
"DD8 1UN\n\n"
"DN551PT\n"
"DN55 1PT\n\n\n\n"
"AN NAA M1 1AA\n"
"ANN NAA M60 1NW\n"
"AAN NAA CR2 6XH\n"
"AANN NAA DN55 1PT\n"
"ANA NAA W1A 1HQ\n"
"AANA NAA EC1A 1BB\n\n"
"DD8 2CA\n"
"DD8 2AA\n"
"DD8 2IA\n"
"DD8 2KA\n"
"DD8 2MA\n"
"DD8 2OA\n"
"DD8 2VA\n"
"DD8 2aa\n\n"
"AN\n"
"ANN\n"
"ANA\n"
"AAN\n"
"AANN\n"
"AANA\n")
subst = "$1 $2"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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 Python, please visit: https://docs.python.org/3/library/re.html