gpt4 book ai didi

python - 用于字母数字文本字符串直至特殊模式的正则表达式

转载 作者:太空宇宙 更新时间:2023-11-03 21:00:20 24 4
gpt4 key购买 nike

我有一个具有特定格式的字符串列表,只需要这些元素的一部分。

输入

my_list = ['The Price Is Right S47E141 720p WEB x264-W4F', 'Breakthrough-The Ideas That Changed the World S01E01 480p x264-mSD',
'The Kid Who Would Be King 2019 DVDR-JFKDVD', 'American Housewife S03E18 Phone Free Day 1080p AMZN WEB-DL DDP5 1 H 264-NTb',
'VICE News Tonight 2019 04 16 720p AMZN WEB-DL DDP2 0 H 264-monkee','The Flash 2014 S05E18 Godspeed 720p AMZN WEB-DL DDP5 1 H 264-NTb',
'The Rachel Maddow Show 2019 04 16 720p MNBC WEB-DL AAC2 0 x264-BTW','Lets Make A Deal 2009 S10E142 XviD-AFG']

正则表达式尝试:

try:
try:
def get_rls(t):
w = re.match(".*\d{4} \d{2} \d{2} ", t)
# w = re.match(".*S\d+E\d+", t)
if not w: raise Exception("Error For Regular Expression")
return w.group(0)

regular_case = [my_list ]
for w in regular_case:
Regular_part = get_rls(w)
print(">>>> Movie Regular Part contains Year/Mon/Day : ", Regular_part)
except:
try:
def get_rls(t):
# w = re.match(".*\d ", t)
w = re.match(".*S\d+E\d+", t)
if not w: raise Exception("Error For Regular Expression")
return w.group(0)


regular_case = [my_list ]
for w in regular_case:
Regular_part = get_rls(w)
print(">>>> Movie Regular Part contains S0E0 : ", Regular_part)

except:
def get_rls(t):
w = re.match(".*\d{4} ", t)
# w = re.match(".*S\d+E\d+", t)
if not w: raise Exception("Error For Regular Expression")
return w.group(0)


regular_case = [my_list ]
for w in regular_case:
Regular_part = get_rls(w)
print(">>>> Movie Regular Part contains Year : ", Regular_part)

except:
print(">>>> Weard Release Name! Pass the Regular part ")
Regular_part = my_list

问题是,我的正则表达式代码只能获取一个元素,并决定使用哪个正则表达式有用并打印正则表达式,并且我需要正则表达式代码能够获取列表并处理每个元素,例如 get第一个元素并决定哪个是好的。

最好的结果应该像下面的列表:

my_list = ['The Price Is Right S47E141', 'Breakthrough-The Ideas That Changed the World S01E01',
'The Kid Who Would Be King 2019 DVDR-JFKDVD', 'American Housewife S03E18 ',
'VICE News Tonight 2019 04 16','The Flash 2014 S05E18',
'The Rachel Maddow Show 2019 04 16 ','Lets Make A Deal 2009 S10E142']

最佳答案

This RegEx不是完整的正确答案,但也许它会帮助您找出处理文本输入的通用方法。也许,RegEx 并不是解决这个问题的最佳方法:

^.+?(?:[SE0-9]+)|(?:\s[A-Z]{4}\-[A-Z]{1,})|(?:.+[0-9]{4}\s[0-9]{2}\s[0-9]{2})|(?:\s[SE0-9]{6,10})

enter image description here

SE 模式和 date 模式对于此正则表达式来说很简单。您可能面临的问题是随机年份,例如 201420092019,您可能会考虑这些年份。

关于python - 用于字母数字文本字符串直至特殊模式的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55752341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com