import re
regex = re.compile(r"Support Escalations*[\s\S]*?Level IV.*", flags=re.MULTILINE)
test_str = ("<td valign=\"top\"><br>\n"
"<br>\n"
"John Doe<br>\n"
"Example, Inc. <br>\n"
"<br>\n"
"Office: 212.777.9999 x295<br>\n"
"jdoe@example.com| transbeam.com <br>\n"
"<br>\n"
"Support Escalations<br>\n"
"Level I: NOC Technician - support@example.com | O: 989.571.1000<br>\n"
"Level II: Manager, Network & Eng Operations, William Tell - wchen@example.com | O: 212.777.9999 x123 | M: 989.265.9876<br>\n"
"Level III: Vice President, Operations, Blaise Pascal - blaisep@example.com | O: 212.777.9999 x234 | M: 343.301.2822 <br>\n"
"Level IV: Chief Operating Officer: BF Skinner - bskinner@example.com | O: 212.777.9999 x345 | M: 343.337.7775<br>\n"
"<br>\n"
" <br>\n"
"<br>\n"
"From: Example Tech Support <br>\n"
"Sent: Monday, August 7, 2017 3:27 PM<br>\n"
"To: Example Tech Support <support@example.com>; 'nocdata@sample.com' <nocdata@sample.com><br>\n"
"Subject: RE: 81434 | WIN - Reparian Riverfront - EoC 10MB - Partial Impact<br>\n"
"<br>\n"
"Sample Support,<br>\n"
"<br>\n"
"Please be advised all loops have been repaired for this customer and they are currently up at their Full Bandiwdth.<br>\n"
"As this is stable, We will close this ticket out.<br>\n"
"<br>\n"
"Please let us know if you have any questions.<br>\n"
"<br>\n"
"Thank you,<br>\n"
"<br>\n"
"John Doe<br>\n"
"Example, Inc. <br>\n"
"<br>\n"
"Office: 212.777.9999 x295<br>\n"
"jdoe@example.com| transbeam.com <br>\n"
"<br>\n"
"Support Escalations<br>\n"
"Level I: NOC Technician - support@example.com | O: 989.571.1000<br>\n"
"Level II: Manager, Network & Eng Operations, William Tell - wchen@example.com | O: 212.777.9999 x123 | M: 989.265.9876<br>\n"
"Level III: Vice President, Operations, Blaise Pascal - blaisep@example.com | O: 212.777.9999 x234 | M: 343.301.2822 <br>\n"
"Level IV: Chief Operating Officer: BF Skinner - bskinner@example.com | O: 212.777.9999 x345 | M: 343.337.7775<br>\n"
"<br>\n"
" <br>\n"
"<br>\n"
"From: Example Tech Support <br>\n"
"Sent: Thursday, August 3, 2017 9:24 AM<br>\n"
"To: Example Tech Support <support@example.com>; 'nocdata@sample.com' <nocdata@sample.com><br>\n"
"Subject: RE: 81434 | WIN - Reparian Riverfront - EoC 10MB - Partial Impact<br>\n"
"<br>\n"
"Correction. The commit on the new ticket (loop 4) is 08/16. Apologies for the error.<br>\n"
"<br>\n"
"-------------------------------------<br>\n"
"Judy Garland<br>\n"
"NOC Technician<br>\n"
"<br>\n"
"From: Example Tech Support <br>\n"
"Sent: Thursday, August 03, 2017 9:22 AM<br>\n"
"To: 'nocdata@sample.com'<br>\n"
"Cc: Example Tech Support<br>\n"
"Subject: 81434 | WIN - Reparian Riverfront - EoC 10MB - Partial Impact<br>\n"
"<br>\n"
"Hello,<br>\n"
"<br>\n"
"Update regarding results of ILEC dispatch to prem this date. ILEC technician repaired the loop that was down (loop 3), but another loop (loop 4) developed a problem while he was there and is now bouncing. Loop 4 has been removed from the efm-group to prevent it from affecting the circuit and a ticket opened with the ILEC for its repair (commit 08/14).<br>\n"
"<br>\n"
"At this time, the 10Mb circuit is up at 9.8Mbps on 3 loops.<br>\n"
"<br>\n"
"<br>\n"
"If you have any further questions, comments or concerns regarding this message, please contact Example NOC support as documented below.<br>\n"
"<br>\n"
"Thank you,<br>\n"
"<br>\n"
"-------------------------------------<br>\n"
"Judy Garland<br>\n"
"NOC Technician<br>\n"
"<br>\n"
"transbeam.com<br>\n"
"<br>\n"
"=======================================================================<br>\n"
"Support Escalations<br>\n"
"Level I: NOC Technician - support@example.com | O: 989.571.1000<br>\n"
"Level II: Manager, Network Operations, William Tell - wchen@example.com | O: 212.777.9999 x123 | M: 989.265.9876<br>\n"
"Level III: Vice President, Operations, Blaise Pascal - blaisep@example.com | O: 212.777.9999 x234 | M: 343.301.2822 <br>\n"
"Level IV: Chief Operating Officer: BF Skinner - bskinner@example.com | O: 212.777.9999 x345 | M: 343.337.7775<br>\n"
"<br>\n\n"
" </td>")
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