Domanda

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 !

È stato utile?

Soluzione

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

re.sub(r'^(\w+_)('')(\w+)$',r'\g<1>1\3',st)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top