import re
regex = re.compile(r"<([a-z]+) .*=\".*\">")
test_str = ("1|<table attribute=\"1\">\n"
"2|<tbody attribute=\"2\">\n"
"3|<tr unwanted_attribute=\"3\"><td unwanted_attribute=\"3\"><br unwanted_attribute=\"3\" /></td></tr>\n"
"4|<tr unwanted_attribute=\"3\"><td unwanted_attribute=\"3\"><span unwanted_attribute=\"3\"></span></td></tr>\n"
"5|</tbody>\n"
"6|</table>")
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