import re
regex = re.compile(r"(?<order>(\d+))\n(?<start>([\d:,]+))[\s-{2}\>]+(?<end>([\d:,]+))\n(?<text>(.+|\n))", flags=re.IGNORECASE)
test_str = ("1\n"
"00:01:35,418 --> 00:01:43,418\n"
"In 2015 an American archeological company\n"
"bought a military manuscript from the Han Dynasty\n\n"
"2\n"
"00:01:45,293 --> 00:01:49,501\n"
"It said\n"
"Roman soldiers went to China 2,000 years ago\n\n"
"3\n"
"00:01:49,918 --> 00:01:53,084\n"
"and the ancient city of Regum was bulit\n\n"
"4\n"
"00:01:53,918 --> 00:01:57,084\n"
"The archeological community believed\n"
"it was a fake")
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