我想删除特定单词“.c:”之后的所有单词、数字、十六进制数字
我的台词是这样的——
line = "Bags has a price.c:123
line = "Bags has a price.c:543ea
我尝试过以下方法:
d = re.sub(r'[.c:\W+]', '', c)
但它没有给出正确的答案,输出将是:
output: Bags has a price
>>> line = "Bags has a price.c:123"
>>> line.split(':')[0]
'Bags has a price.c'
>>> line.split('.c')[0]
'Bags has a price'
>>> line = "Bags has a price.c:123"
>>> ''.join(line.partition('.c')[:2])
'Bags has a price.c'
并且只使用一个简单的索引查找。
>>> line = "Bags has a price.c:543ea"
>>> after_word = ".c"
>>> cleaned_line = line[:line.index(after_word) + len(after_word) ]
>>> cleaned_line
Bags has a price.c
要排除.c
只是删除+ len(after_word)
如果你必须使用正则表达式——显然你不需要。你可以这样做:
re.sub(r'\.c:.*?$','', line)
如果您可以避免使用正则表达式,请执行此操作。使用拆分可能会慢得多。