删除特定单词之后的所有单词

3

我希望能够删除指定单词".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
4个回答

5
>>> line = "Bags has a price.c:123"
>>> line.split(':')[0]
'Bags has a price.c'
>>> line.split('.c')[0]
'Bags has a price'

2
>>> line = "Bags has a price.c:123"
>>> ''.join(line.partition('.c')[:2])
'Bags has a price.c'

这比使用.split方法更加简洁。在我看来,.partition通常被忽视了。 - Karl Knechtel

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)


0

如果你必须使用正则表达式 - 虽然显然你不需要。你可以这么做:

re.sub(r'\.c:.*?$','', line)

如果可以避免使用正则表达式,就尽量避免。使用 split 函数可能会更快。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接