I have a s="gh3wef2geh4ht"
. How can I receive s="gh333wef22geh4444ht"
by using sub. I have tried this regexp. what I am doing wrong?
s=re.sub(r"(\d)",r"\1{\1}",s)
You can use a lambda
function to capture the matched digits and repeat it:
s="gh3wef2geh4ht"
re.sub(r'(\d)', lambda m: m.group(1) * int(m.group(1)), s)
# 'gh333wef22geh4444ht'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With