import re
regex = re.compile(r"[ ]*List of \d+ base pairs([\s\S]*?)(?=\*{5,})")
test_str = (" bla bla bla \n"
" bla some on wanted text....\n\n"
"****************************************************************************\n"
"List of 12 base pairs\n"
" nt1 nt2 bp name Saenger LW DSSR\n"
" 1 Q.C0 Q.G22 C-G WC 19-XIX cWW cW-W\n"
" 2 Q.C1 Q.G21 C-G WC 19-XIX cWW cW-W\n"
" 3 Q.U2 Q.A20 U-A WC 20-XX cWW cW-W\n\n"
"****************************************************************************\n"
"another unwanted text ...\n"
"another unwanted text ")
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