python re.sub 组: umber 之后的数字

2022-01-17 00:00:00 python regex numbers regex-group

问题描述

如何将 foobar 替换为 foo123bar?

这不起作用:

>>> re.sub(r'(foo)', r'1123', 'foobar')
'J3bar'

这行得通:

>>> re.sub(r'(foo)', r'1hi', 'foobar')
'foohibar'

我认为当有 umber 之类的内容时,这是一个常见问题.谁能给我一个关于如何处理这个问题的提示?

I think it's a common issue when having something like umber. Can anyone give me a hint on how to handle this?


解决方案

答案是:

re.sub(r'(foo)', r'g<1>123', 'foobar')

文档的相关摘录:

除了字符转义和如上所述的反向引用,g 将使用子字符串由名为 name 的组匹配,如由 (?P...) 语法定义.g 使用对应的组号;g<2> 因此是等价于 2,但不是模棱两可的在诸如g<2>0之类的替换中.20将被解释为参考第 20 组,不是对第 2 组的引用后跟文字字符0".反向引用 g<0> 替换为匹配的整个子字符串回复.

In addition to character escapes and backreferences as described above, g will use the substring matched by the group named name, as defined by the (?P...) syntax. g uses the corresponding group number; g<2> is therefore equivalent to 2, but isn’t ambiguous in a replacement such as g<2>0. 20 would be interpreted as a reference to group 20, not a reference to group 2 followed by the literal character '0'. The backreference g<0> substitutes in the entire substring matched by the RE.

相关文章