我在学习python时遇到了re.match问题
编码如下:
def pythonReSubDemo():
inputstr='hello 111'
def add(matched):
intvalue=matched.group(0)
...
re.sub('\d+',add,inputstr)
没有使用匹配匹配任何正则表达式为什么可以使用match.group()
答案 0 :(得分:1)
答案 1 :(得分:0)
使用以下语法命名组:
(?P<number> ... )
re.sub('(?P<number>\d+)', add, inputstr)
>>> def add(matched):
... return str(int(matched.group('number')) + 1)
...
>>> inputstr='hello 111'
>>> re.sub('(?P<number>\d+)', add, inputstr)
'hello 112'