218

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?

Carlos Mermingas
  • 3,402
  • 2
  • 17
  • 38
zhigang
  • 5,128
  • 4
  • 25
  • 20
  • 2
    This question has been added to the [Stack Overflow Regular Expression FAQ](http://stackoverflow.com/a/22944075/2736496), under "Groups". – aliteralmind Apr 10 '14 at 00:24
  • 1
    this question took me quite a long time to find, because it doesn't feature the terms 'capture group' or 'numbered group reference', but I'm here eventually and glad you asked it. – Mark Ch Jun 20 '19 at 09:02
  • 1
    Your issue is that r'\112' is getting interpreted as the octal literal 0112, ASCII'J', or decimal 74. Can't see how to force the backreference '\1' to get evaluated before string concatenation or `''.join()` – smci Jul 20 '19 at 22:56
  • a small deviation from the question, any way to refer all group matches i.e. r'\hi'? –  Jun 18 '20 at 09:37

1 Answers1

370

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.

John Gaines Jr.
  • 9,766
  • 1
  • 23
  • 25
  • 60
    Don't be so hard on yourself. It's [buried in the documentation](https://docs.python.org/2/library/re.html#module-contents) so far deep that it would take most people far more time to read the docs than to google their question and have this answer come up on SO. – speedplane Sep 01 '15 at 05:55
  • 1
    The exact quote provided is found [here](https://docs.python.org/3.1/library/re.html#re.sub) in case you are looking for context – patrick May 17 '18 at 14:58
  • Can I take the group and modify it? \g<1> ... For example in this case g<1> is foo, but I wanna change the o by u, like this "fuu" – Eric Bellet Jul 03 '20 at 09:02