umaru
umaru

Reputation: 178

python re.sub() with ?P

Can I use a variable from the first argument of re.sub (), to use it in the second argument? Let me explain with an example:

re.sub(r'(?P<id>>>>[0-9]+)', 'sometext(?P=id)sometext', self.text))

Can I use id variable in 'sometext(?P=id)sometext'? Actually, this code dont work, so i came here.

Upvotes: 0

Views: 507

Answers (1)

alex
alex

Reputation: 490423

You can refer to a capturing group by number, e.g. the first capturing group would be \1.

Upvotes: 2

Related Questions