Question

So I was replacing characters by re module.
I have a string 'abc_def' and need to add 1 after _.
So I was doing this.

st = 'abc_def'
re.sub(r'^(\w+_)('')(\w+)$',r'\11\3',st)

But this takes \11 as 11th captured group, not \1 and 1 separately.

Btw r\1,1\3 works just as it should, returns abc_,1def.

Need help !

Était-ce utile?

La solution

You can use \g<number> instead of \number:

re.sub(r'^(\w+_)('')(\w+)$',r'\g<1>1\3',st)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top