質問

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 !

役に立ちましたか?

解決

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

re.sub(r'^(\w+_)('')(\w+)$',r'\g<1>1\3',st)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top