import re
# 将匹配的数字乘以 2
def double(matched):
value = int(matched.group('value'))
return str(value * 2)
s = 'A23G4HFD567'
print(re.sub('(?P<value>\d+)', double, s))
?P的意思就是命名一个名字为value的组,匹配规则符合后面的\d+
2023-12-23 13:35:33
import re
# 将匹配的数字乘以 2
def double(matched):
value = int(matched.group('value'))
return str(value * 2)
s = 'A23G4HFD567'
print(re.sub('(?P<value>\d+)', double, s))
?P的意思就是命名一个名字为value的组,匹配规则符合后面的\d+