Регулярное выражение здесь уместно - ищите «S», затем некоторое количество цифр, затем «E» и некоторое количество цифр.
import re
def get_title(s):
m = re.match(".*S\d+E\d+", s)
if not m: raise Exception("Couldn't find S<num>E<num> substring")
return m.group(0)
test_cases = [
"Drop the Mic S02E16 1080p WEB x264-TBS",
"Hughesy We Have A Problem S03E05 720p HDTV x264-CBFM",
"Dead of Winter S01E03 Winters Bone 1080p WEB x264-UNDERBELLY",
"Saturday Night Live S44E14 John Mulaney 720p WEB x264-TBS"
]
for s in test_cases:
print(s, "====>", get_title(s))
Результат:
Drop the Mic S02E16 1080p WEB x264-TBS ====> Drop the Mic S02E16
Hughesy We Have A Problem S03E05 720p HDTV x264-CBFM ====> Hughesy We Have A Problem S03E05
Dead of Winter S01E03 Winters Bone 1080p WEB x264-UNDERBELLY ====> Dead of Winter S01E03
Saturday Night Live S44E14 John Mulaney 720p WEB x264-TBS ====> Saturday Night Live S44E14
Если вам нужен индивидуальный доступ к каждой части заголовка, вы можете использовать группы захвата:
import re
def get_info(s):
m = re.match("(.*)(S\d+E\d+)(.*)", s)
if not m: raise Exception("Couldn't find S<num>E<num> substring")
return m.groups()
test_cases = [
"Drop the Mic S02E16 1080p WEB x264-TBS",
"Hughesy We Have A Problem S03E05 720p HDTV x264-CBFM",
"Dead of Winter S01E03 Winters Bone 1080p WEB x264-UNDERBELLY",
"Saturday Night Live S44E14 John Mulaney 720p WEB x264-TBS"
]
for s in test_cases:
title, episode_num, format_info = get_info(s)
print(s)
print("\tTitle:", title)
print("\tEpisode Number:", episode_num)
print("\tFormat Info:", format_info)
Результат:
Drop the Mic S02E16 1080p WEB x264-TBS
Title: Drop the Mic
Episode Number: S02E16
Format Info: 1080p WEB x264-TBS
Hughesy We Have A Problem S03E05 720p HDTV x264-CBFM
Title: Hughesy We Have A Problem
Episode Number: S03E05
Format Info: 720p HDTV x264-CBFM
Dead of Winter S01E03 Winters Bone 1080p WEB x264-UNDERBELLY
Title: Dead of Winter
Episode Number: S01E03
Format Info: Winters Bone 1080p WEB x264-UNDERBELLY
Saturday Night Live S44E14 John Mulaney 720p WEB x264-TBS
Title: Saturday Night Live
Episode Number: S44E14
Format Info: John Mulaney 720p WEB x264-TBS