我不知道我该如何解释这一点,但这里是......
>>> x = 'qwertyHelloWorldasdfg'
>>> x = inversereplace(x, 'HelloWorld', 'a')
>>> print x
aaaaaaHelloWorldaaaaaa
>>> y = 'qwertyHelloWorld'
>>> y = inversereplace(y, 'qwerty', '')
>>> print y
qwerty
在上面的函数中,它用第三个参数替换了 x 中不是不是第二个参数的所有内容。
我该怎么做呢?
如果已经有执行此操作的函数,请告诉我。
提前致谢,
~DragonXDoom
最佳答案
这应该有效:
def inversereplace(text, word, repl):
parts = text.split(word)
return word.join(repl*len(x) for x in parts)
关于Python:反向替换函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4676700/