>>> help(re.sub)
1 Help on function sub in module re:
2
3 sub(pattern, repl, string, count=0)
4 Return the string obtained by replacing the leftmost
5 non-overlapping occurrences of the pattern in string by the
6 replacement repl. repl can be either a string or a callable;
7 if a callable, it's passed the match object and must return
8 a replacement string to be used.
没有功能正则表达式标记(IGNORECASE, MULTILINE, DOTALL
)中的参数re.sub
,如re.compile
。
替代方案:
>>> re.sub("[M|m]r", "", "Mr Bean")
' Bean'
>>> re.sub("(?i)mr", "", "Mr Bean")
' Bean'
编辑 Python 3.1中,增加了对正则表达式的标志,http://docs.python.org/3.1/whatsnew/3.1.html。从3.1开始, re.sub
样子:
re.sub(pattern, repl, string[, count, flags])