python re. sub of Replace Regular Matching Content Method
- 2021-07-24 11:24:15
- OfStack
As shown below:
import re
c = re.compile(r'\d')
s = 'you1are2welcome'
# Replaces the contents of the regular match with the specified contents, or you can specify the number of replacements
ret = c.sub(' ', s, 1)
print(ret)
# Handler function receive 1 Parameters ( The result of each match )
def deal(s):
return str(int(s.group()) * 2)
# It can be considered as intervening in the replacement process, passing 1 One function is enough
ret = re.sub(r'\d', deal, 'you1are2welcome')
print(ret)
Results:
you are2welcome
you2are4welcome