import re
regex = re.compile(r"e(?:pisode)?\s*(\d{1,3}(?!\d)|\d\d\d??)(?:-?e?(\d{1,3}))?(?!\d)", flags=re.MULTILINE)
test_str = ("some.random.tv.show.s01episode01.1080p\n"
"some.random.tv.show.s01episode 01.1080p\n"
"some.random.tv.show.s01episode1.1080p\n"
"some.random.tv.show.s01episode 1.1080p\n"
"some.random.tv.show.s01e01.1080p\n"
"some.random.tv.show.s01e1.1080p\n"
"some.random.tv.show.s01e0102.1080p\n"
"some.random.tv.show.s01e01-02.1080p\n"
"some.random.tv.show.s01e01e02.1080p\n"
"some.random.tv.show.s01e01-e02.1080p\n"
"some.random.tv.show.s01e199.1080p\n"
"some.random.tv.show.s01e001004.1080p\n"
"some.random.tv.show.s01e1-e2.1080p\n"
"some.random.tv.show.s01e001-e002.1080p")
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