re.sub: escaping capture group followed by numeric(s)

MRAB python at
Fri Sep 17 20:56:37 CEST 2010

On 17/09/2010 19:21, Jon Clements wrote:
> Hi All,
> (I reckon this is probably a question for MRAB and is not really
> Python specific, but anyhow...)
> Absolutely basic example: re.sub(r'(\d+)', r'\1', 'string1')
> I've been searching around and I'm sure it'll be obvious when it's
> pointed out, but how do I use the above to replace 1 with 11?
> Obviously I can't use r'\11' because there is no group 11. I know I
> can use a function to do it, but it seems to me there must be a way
> without. Can I escape r'\11' somehow so that it's group 1 with a '1'
> after it (not group 11).
re.sub(r'(\d+)', r'\g<1>', 'string1')

More information about the Python-list mailing list