我希望能够删除指定单词".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)
.split
方法更加简洁。在我看来,.partition
通常被忽视了。 - Karl Knechtel