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

4 回答 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'
于 2012-12-20T07:09:01.300 回答
2
>>> line = "Bags has a price.c:123"
>>> ''.join(line.partition('.c')[:2])
'Bags has a price.c'
于 2012-12-20T07:01:18.490 回答
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)

于 2012-12-20T07:59:05.857 回答
0

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

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

如果您可以避免使用正则表达式,请执行此操作。使用拆分可能会慢得多。

于 2012-12-20T07:26:34.740 回答