python re.sub group: number after \number

后端 未结 1 1566
无人共我
无人共我 2020-11-22 14:19

How can I replace foobar with foo123bar?

This doesn\'t work:

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


        
相关标签:
1条回答
  • 2020-11-22 14:39

    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.

    0 讨论(0)
提交回复
热议问题