我想通过正则表达式得到字符串的一部分,我尝试了这个方法,但它返回的结果比我需要的要多。这是我的代码:
Release_name = 'My Kitchen Rules S10E35 720p HDTV x264-ORENJI'
def get_rls(t):
w = re.match(".*\d ", t)
if not w: raise Exception("Error For Regular Expression")
return w.group(0)
regular_case = [Release_name]
for w in regular_case:
Regular_part = get_rls(w)
print(">>>> Regular Part: ", Regular_part)
此示例的代码“My Kitchen Rules S10E35 720p HDTV x264-ORENJI
”
返回这个“My Kitchen Rules S10E35
”,但我不需要“S10E35
”,只需返回这个My Kitchen Rules
你可以用
由于您需要的值在组1中:
^{pr2}$请参见Python demo,输出是
>>>> Regular Part: My Kitchen Rules
。在细节
(.*?)
-除换行符外的任何0+字符,尽可能少\s+
-1+个空格S\d+E\d+
-S
字符,1+位数,E
和1+位数re.match
将只从字符串的开头开始匹配,^
不需要在模式的开头。在相关问题 更多 >
编程相关推荐