📜  单个正则表达式中的多个子 - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:33.326000             🧑  作者: Mango

代码示例1
# Multiple sub in single regex.
import re
class Substitutable(str):
  def __new__(cls, *args, **kwargs):
    newobj = str.__new__(cls, *args, **kwargs)
    newobj.sub = lambda fro,to: Substitutable(re.sub(fro, to, newobj))
    return newobj
h = Substitutable('horse')
h = h.sub('h', 'f').sub('e','h')
print(h)