根据我是否预编译正则表达式,我得到了不同的结果:
>>> re.compile('mr', re.IGNORECASE).sub('', 'Mr Bean')
' Bean'
>>> re.sub('mr', '', 'Mr Bean', re.IGNORECASE)
'Mr Bean'
Run Code Online (Sandbox Code Playgroud)
在Python文档说,有些功能是用于编译正则表达式的功能齐全的方法简化版本.但是它也声称RegexObject.sub()与sub()函数相同.
那么这里发生了什么?
Eva*_*ark 12
re.sub()re.IGNORECASE它似乎无法接受.
文件说明:
sub(pattern, repl, string, count=0)Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the replacement repl. repl can be either a string or a callable; if a string, backslash escapes in it are processed. If it is a callable, it's passed the match object and must return a replacement string to be used.
然而,使用它可以取代它:
re.sub("(?i)mr", "", "Mr Bean")
Run Code Online (Sandbox Code Playgroud)