import re
regex = re.compile(r"(<\w+)(\s*[\w-]+=\"[^\"]+\")*(\s*[\w-]+=\"[^\"]+\")*(\s*>)|(\w)|(<\/\w+)*(\s*>)")
test_str = ("<td class=\"sky\" data-toggle=\"up\" id=\"heaven\" >\n"
"<td class=\"sky\" data-toggle=\"up\" id=\"heaven\" >\n"
"<td class=\"sky\" data-toggle=\"up\" id=\"heaven\" >\n\n"
"<td class=\"sky\" data-toggle=\"up\" id=\"heaven\" >\n\n"
"<td class=\"sky\" data-toggle=\"up\" id=\"heaven\" >\n\n"
"<td class=\"sky\" data-toggle=\"up\" id=\"heaven\" >\n"
"<td class=\"sky\" data-toggle=\"up\" id=\"heaven\" >\n"
"<td class=\"sky\" data-toggle=\"up\" id=\"heaven\" >\n\n"
"<a href=\"#\" data-toggle=\"modal\" data-target=\"xxxx\">xxxx</a>\n\n"
"<a href=\"#\"data-target=\"#available-assistance\" data-toggle=\"modal\" data-modal=\"yeah\"></a>\n\n"
"<a href=\"#\"data-target=\"#available-assistance\" data-toggle=\"modal\" data-modal=\"yeah\"></a>\n\n")
subst = "$1$4$5$6$7"
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