Python中是否有一种快速的方法来替换字符串,但是从头开始,而不是从头开始replace
?例如:
>>> def rreplace(old, new, occurrence)
>>> ... # Code to replace the last occurrences of old by new
>>> '<div><div>Hello</div></div>'.rreplace('</div>','</bad>',1)
>>> '<div><div>Hello</div></bad>'
Run Code Online (Sandbox Code Playgroud) 假设我有这段文字:
Saturday and Sunday and Monday and Tuesday and Wednesday and Thursday and Friday are days of the week.
Run Code Online (Sandbox Code Playgroud)
我希望除了最后and
一个用逗号代替之外的所有内容:
Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday and Friday are days of the week.
Run Code Online (Sandbox Code Playgroud)
在正则表达式中有一个简单的方法吗?据我所知,replace
正则表达式中的方法一直替换字符串.