import re
regex = re.compile(r"<\s*(/?)\s*(?=(?:a|strong|em)\b)(\w+[^>]*)>")
test_str = ("<a href=\"https://www.example.com/page\">Example page</a> is a <strong>valid</strong> tag.\n"
"< em class=\"something\"> is also allowed</em>\n"
"But <param> or <argument> should be escaped.\n"
"This also: <br/> <\n"
"br /> <script type=\"text/javascript\">alert('xss'></script>\n\n"
"Turn left <- or turn right ->\n"
"Also, accept this => or a smiley >.<")
subst = "[\\1\\2]"
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