python re.sub组:\number之后的数字 [英] python re.sub group: number after \number

查看:66
本文介绍了python re.sub组:\number之后的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 foobar 替换为 foo123bar?

这不起作用:

<预><代码>>>>re.sub(r'(foo)', r'\1123', 'foobar')'J3bar'

这有效:

<预><代码>>>>re.sub(r'(foo)', r'\1hi', 'foobar')'foohibar'

我认为在使用 \number 之类的东西时,这是一个常见问题.谁能告诉我如何处理这个问题?

解决方案

答案是:

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> 替代匹配的整个子串重新.

How can I replace foobar with foo123bar?

This doesn't work:

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

This works:

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

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

解决方案

The answer is:

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

Relevant excerpt from the docs:

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.

这篇关于python re.sub组:\number之后的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆