Python - 替换所有单词开头

时间:2014-11-21 15:37:37

标签: python replace

我想知道如何删除以" saison"开头的所有单词。

例如:

test = "This is an example of saison1, saison7 and saison58 could be deleted too"
#test = test.replace("saison1", "")
#test = test.replace("saison58", "")

拥有:

test = "This is an example of ,  and  could be deleted too"

怎么做?

2 个答案:

答案 0 :(得分:3)

您可以使用正则表达式:

import re

test = re.sub(r'\bsaison\d*\b', '', test)

这会删除文本saison后面出现test的0位或更多位数字。开头和结尾的\b确保您只匹配整个单词,而不是匹配中间或结尾只有包含 saison(后跟数字)的单词,或者从saison开始,但以其他内容结束。

演示:

>>> import re
>>> test = "This is an example of saison1, saison7 and saison58 could be deleted too"
>>> re.sub(r'\bsaison\d*\b', '', test)
'This is an example of ,  and  could be deleted too'

答案 1 :(得分:0)

另一种解决方案:

>>> ' '.join([ word for word in test.split() if not word.startswith('saison') ])
'This is an example of and could be deleted too'