Python正则表达式不一致

2024-05-20 03:43:10 发布

您现在位置:Python中文网/ 问答频道 /正文

根据是否预编译正则表达式,我得到了不同的结果:

>>> re.compile('mr', re.IGNORECASE).sub('', 'Mr Bean')
' Bean'
>>> re.sub('mr', '', 'Mr Bean', re.IGNORECASE)
'Mr Bean'

Python documentation表示一些函数是编译正则表达式的全功能方法的简化版本。但它也声称RegexObject.sub()与sub()函数相同。在

这是怎么回事?在


Tags: 方法函数版本redocumentation全功能mrcompile
3条回答

似乎,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")

模块级sub()调用在末尾不接受修饰符。这是“count”参数-要替换的模式出现的最大数量。在

>>> 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.

对于regex标志(IGNORECASE, MULTILINE, DOTALL),在re.sub中没有函数参数。在

备选方案:

^{pr2}$

EditPython3.1增加了对regex标志http://docs.python.org/3.1/whatsnew/3.1.html的支持。从3.1开始,例如^{}的签名如下:

re.sub(pattern, repl, string[, count, flags])

相关问题 更多 >