"re.sub" 带有“.*”的方法

"re.sub" method with ".*"

我在使用 python re 库时遇到了以下行为。

>>> import re
>>> re.sub(pattern=".*", repl="r", string="hello")
'rr'

如您所见,对于模式 .* 和替换字符 (r) re.sub 方法返回 rr。但我期望结果为 r 因为 .* 会匹配整个字符串。这是为什么?。我也在 Go 中测试了相同的逻辑,但它返回了预期的结果。

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile(`.*`)
    fmt.Println(re.ReplaceAllString("Hello", "r")) // Will print `r`
}

下面应该开始解释是怎么回事了:

>>> re.sub("x?", "_", "hello")
'_h_e_l_l_o_'

在字符串的每个位置 re.sub 尝试匹配 x?。成功了,因为x?可以匹配到空串,将空串替换成_.

以类似的方式,在下面

>>> re.sub(".*", "r", "hello")
'rr'

我们有 re.sub 尝试匹配位置 0 的 .*,成功并消耗了整个字符串。然后它尝试匹配结束位置,成功(匹配空字符串)并再次将其替换为 r 。如果您不允许空匹配,'puzzling' 行为就会消失:

>>> re.sub(".+", "r", "hello")
'r'

在 Python 3.7 之前的版本中,如果 re.sub 消耗了整个字符串,它将不再尝试匹配末尾,而在 Python 3.7+ 中它会。更具体地说,引用re.sub的文档:

Changed in version 3.7: Empty matches for the pattern are replaced when adjacent to a previous non-empty match.

Python 3.7+(行为一致)

>>> matches = lambda r, s: [m.span() for m in re.finditer(r, s)]
>>> matches("x?", "x")
[(0, 1), (1, 1)]
>>> matches("x?", "y")
[(0, 0), (1, 1)]
>>> re.sub("x?", "r", "x")
'rr'
>>> re.sub("x?", "r", "y")
'ryr

Python 3.6(行为不一致)

>>> matches("x?", "x")
[(0, 1), (1, 1)]
>>> matches("x?", "y")
[(0, 0), (1, 1)]
>>> re.sub("x?", "r", "x")
'r'
>>> re.sub("x?", "r", "y")
'ryr'