def patch_re_module():
"""
Modify the standard ``re`` module by installing new versions of
the functions ``re.compile``, ``re.sub``, and ``re.subn``,
causing regular expression substitutions to return
``SourcedStrings`` when called with ``SourcedStrings``
arguments.
Use this function only if necessary: it potentially affects
all Python modules that use regular expressions!
"""
def new_re_sub(pattern, repl, string, count=0):
return re.compile(pattern).sub(repl, string, count)
def new_re_subn(pattern, repl, string, count=0):
return re.compile(pattern).subn(repl, string, count)
re.compile = SourcedStringRegexp
re.sub = new_re_sub
re.subn = new_re_subn
评论列表
文章目录