import re
regex = re.compile(r"(?<Fname>.*\.)(S(?<se>\d{1,2})E(?<ep>\d{2,3}).*|(?<yr>\d{4}[^p]).*)", flags=re.MULTILINE)
test_str = ("Westworld.S04E05.720p.WEB.h264-KOGi[rarbg]\n"
"Westworld.S04E04.1080p.WEB.H264-CAKES[rarbg]\n"
"For.All.Mankind.S03E08.720p.WEB.h264-KOGi[rarbg]\n"
"Talk.to.Me.2022.1080p.WEBRip.DD5.1.x264-LAMA\n"
"Jules.2023.1080p.AMZN.WEBRip.DD5.1.x264-LAMA\n\n")
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