其他分享
首页 > 其他分享> > 正则表达式 ?P<name>

正则表达式 ?P<name>

作者:互联网

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+

标签:匹配,name,正则表达式,double,value,re,int,matched
来源: https://blog.csdn.net/m0_46172703/article/details/118815557