我想删除特定单词“.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
答案 0 :(得分:5)
>>> line = "Bags has a price.c:123"
>>> line.split(':')[0]
'Bags has a price.c'
>>> line.split('.c')[0]
'Bags has a price'
答案 1 :(得分:2)
>>> line = "Bags has a price.c:123"
>>> ''.join(line.partition('.c')[:2])
'Bags has a price.c'
答案 2 :(得分:0)
如果你必须使用正则表达式 - 显然你没有。你可以做到这一点:
re.sub(r'\.c:.*?$','', line)
如果你可以避免使用正则表达式,那么就做。使用拆分可能要慢得多。
答案 3 :(得分:0)
只需使用简单的索引查找。
>>> 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)