import re
regex = re.compile(r"(?<=>)\s*([^<]*[^\s<])\s*(?=<)|(?<=>)\s*", flags=re.MULTILINE)
test_str = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<note>\n"
" <to>Tove</to>\n"
" <from>Jani</from>\n"
" <heading> * + Reminder + * </heading>\n"
" <body>Don't forget\n"
" me this weekend! \n"
" </body>\n"
"</note> \n\n")
subst = "$1"
result = regex.sub(subst, test_str)
if result:
print(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 Python, please visit: https://docs.python.org/3/library/re.html