【问题标题】:"re.sub" method with ".*"带有“.*”的“re.sub”方法
【发布时间】:2020-12-31 04:21:05
【问题描述】:

我在使用 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`
}

【问题讨论】:

标签: python regex replace python-re python-3.9


【解决方案1】:

下面应该开始解释发生了什么:

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

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

以类似的方式,在下面

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

我们有 re.sub 尝试在位置 0 匹配 .*,成功,并消耗整个字符串。然后它尝试在结束位置匹配,成功(匹配空字符串)并再次将其替换为r。如果您禁止空匹配,“令人费解”的行为就会消失:

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

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

Changed in version 3.7:模式的空匹配在与之前的非空匹配相邻时被替换。

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'

【讨论】:

  • 很好的答案。包含 Python 的正则表达式引擎实际上最近改变了其行为的参考可能会很好。
  • @TimBiegeleisen 已添加,并附有文档中的相关引用。
猜你喜欢
  • 2014-06-24
  • 1970-01-01
  • 2012-06-13
  • 1970-01-01
  • 2010-09-07
  • 1970-01-01
相关资源
最近更新 更多