6

What would be the syntax if I want to insert "0" after the first group reference ?

import re
re.sub("(..)(..)", "\\1x\\2", "toto")
toxto
re.sub("(..)(..)", "\\10\\2", "toto")
sre_constants.error: invalid group reference

Error, because \10 is interpreted as 10th reference group (that's why in ed(), group references are in [1-9] interval).

In the example above, how to obtain "to0to" ?

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Eric H.
  • 2,152
  • 4
  • 22
  • 34

1 Answers1

13

You can use \g based group substitution:

>>> import re
>>> re.sub("(..)(..)", r"\g<1>0\g<2>", "toto")
'to0to'

From docs:

\g<number> 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'.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • 2
    You may want to quote the [documentation](http://docs.python.org/2/library/re.html#re.sub): *`\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`.* – Martijn Pieters Jan 13 '14 at 12:30
  • That explains every issue I had in re.sub and required usage of lambda function. Thanks a lot! – Jean-Francois T. Nov 01 '17 at 08:55