문제

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