import re
regex = re.compile(r"((?:<p[^>]*?color: black.*?>[\S\s\n]*?<\/p>\s*<span[^>]*>)|(?:<span[^>]*?color: black.*?>[\S\s\n]*?<code))(*SKIP)(*F)|<code\s*style=\"background-color:\s*transparent;\">", flags=re.MULTILINE)
test_str = ("<html>\n"
"<p style=\"font-family: "verdana"; font-size: 18px; color: black; line-height: 18px; text-align: justify; font-style: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: cyan;\"><span style=\"font-size: 13.5pt; font-family: "Verdana","sans-serif";\"><code style=\"background-color: transparent;\"><b>some text here</b></code></span></p>\n"
"<span><span style=\"font-size: 13.5pt; font-family: "Verdana","sans-serif"; background-color: cyan;\"><code style=\"background-color: transparent;\"><b>some text here</b></code></span>\n\n"
"<code style=\"background-color: transparent;\">\n\n"
"<p style=\"font-family: "verdana"; font-size: 18px; color: cyan; line-height: 18px; text-align: justify; font-style: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: cyan;\"><span style=\"color: black; font-size: 13.5pt; font-family: "Verdana","sans-serif";\"><code style=\"background-color: transparent;\"><b>some text here</b></code></span></p>\n"
"<span><span style=\"font-size: 13.5pt; font-family: "Verdana","sans-serif"; background-color: cyan;\"><code style=\"background-color: transparent;\"><b>some text here</b></code></span>\n"
"<p style=\"font-family: "verdana"; font-size: 18px; color: cyan; line-height: 18px; text-align: justify; font-style: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; background-color: navy;\"><span style=\"font-size: 13.5pt; font-family: "Verdana","sans-serif";\"><code style=\"background-color: transparent;\"><b>some text here</b></code></span></p>\n"
"</html>")
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