import re
regex = re.compile(r"((<(p|h3.*)>.*<\/(.*)>)|(<(ul.*)>[\s\S]*<\/(ul)>))", flags=re.MULTILINE)
test_str = ("<h1>This is heading 1</h1>\n"
"<h2 style=\"color: aqua\">This is heading 2</h2>\n"
"<h3>This is heading 3</h3>\n"
"<p>This is a paragraph.</p>\n"
"<p>This is another paragraph.</p>\n"
"<a href=\"https://www.w3schools.com\">This is a link</a>\n"
"<ul>\n"
" <li>Coffee</li>\n"
" <li>Tea</li>\n"
" <li>Milk</li>\n"
"</ul>")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
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