说我有一个字符串:
"the quick brown fox jumped over the moon.this text needs to be removed."
我正在尝试使用Python删除".this text needs to be removed."
。
我尝试了多种方法,主要由\w+(\..*\.)
组成,但它不起作用。我需要一种通用的方法来删除最后一部分,因为每个文件的文本都不同,所以像re.sub('\.this text needs to be removed\.', '', string)
这样的东西对我来说不起作用。
答案 0 :(得分:1)
你的正则表达式应如下所示:
re.sub(r'\.[^.]*?\.$', '', someString)
这将确保re.sub
仅匹配字符串末尾的句点之间的文本。如果没有$
,它将匹配字符串中的任何匹配句点集。
修改强>
如果您想在点之间捕获所有:\..*\.
答案 1 :(得分:1)
我同意abhijit,为什么不使用字符串函数?例如:
s1="the quick brown fox jumped over the moon.this text needs to be removed."
s2=s1.replace(".this text needs to be removed.","")
虽然正则表达式非常强大,但字符串对象上的方法通常会针对性能进行优化。
答案 2 :(得分:1)
阅读你的问题,你可以实现你想要的目标:
str = 'the quick brown fox jumped over the moon.this text needs to be removed.'
str = str.split('.this text needs to be removed.', 1)
print str[0] /* it prints "the quick brown fox jumped over the moon" */
答案 3 :(得分:0)
你忘了逃避.
,并犯了其他一些错误。这应该有效:
s = "the quick brown fox jumped over the moon.this text needs to be removed."
s = re.sub("\..*\.", "", s)
答案 4 :(得分:0)
如果你想用正则表达式做这个,只需使用sub。
>>> re.sub("\.this text needs to be removed\.","","the quick brown fox jumped over the moon.this text needs to be removed.")
'the quick brown fox jumped over the moon'
但是,使用Python字符串功能可以完成这么简单的任务
>>> "the quick brown fox jumped over the moon.this text needs to be removed.".replace(".this text needs to be removed.","")
'the quick brown fox jumped over the moon'
删除最后一句话的一般方法是
>>> re.sub("\.[^\.]+","","the quick brown fox jumped over the moon.this text needs to be removed.")
'the quick brown fox jumped over the moon.'
没有正则表达式
>>> ''.join("the quick brown fox jumped over the moon.this text needs to be removed.".rsplit('.',2)[:-2])
'the quick brown fox jumped over the moon'
>>>