所以现在,re.sub
会这样做:
>>> re.sub("DELETE THIS", "", "I want to DELETE THIS472 go to DON'T DELETE THIS847 the supermarket")
"I want to go to DON'T the supermarket"
我希望它只删除“delete THISXXX”的第一个实例,其中XXX是一个数字,因此结果是
^{pr2}$
XXX是一个变化的数字,所以我确实需要一个正则表达式。我怎样才能做到这一点?在
Tags:
可选参数count是要替换的模式出现的最大数量;count必须是非负整数。在
将count=1设置为仅替换第一个实例。在
正如在
re.sub(pattern, repl, string, count=0, flags=0)
的documentation中所写的那样,您可以在中指定count
参数:如果你只给出1的计数,它只会替换第一个
来自http://docs.python.org/library/re#re.sub:
相关问题 更多 >
编程相关推荐