Gut*_*ilz 0 python regex replace
我有一个字符串,它有一个我想匹配和替换的子字符串.
movie.2002.german.720p.x264-msd...
Run Code Online (Sandbox Code Playgroud)
我想删除x264-blblxcv.此行无法按预期工作.
title = title.replace('.x264-\S+','')
Run Code Online (Sandbox Code Playgroud)
str.replace()它不支持正则表达式.您只能使用该方法替换文字文本,并且输入字符串不包含文字文本.x264-\S+.
使用该re.sub()方法执行您想要的操作:
import re
title = re.sub(r'\.x264-\S+', '', title)
Run Code Online (Sandbox Code Playgroud)
演示:
>>> import re
>>> title = 'movie.2002.german.720p.x264-msd...'
>>> re.sub(r'\.x264-\S+', '', title)
'movie.2002.german.720p'
Run Code Online (Sandbox Code Playgroud)
或者,分区.x264-为str.partition():
title = title.partition('.x264-')[0]
Run Code Online (Sandbox Code Playgroud)