python match.group()但不匹配

时间:2016-04-04 04:44:29

标签: python regex

我在学习python时遇到了re.match问题 编码如下: def pythonReSubDemo(): inputstr='hello 111' def add(matched): intvalue=matched.group(0) ... re.sub('\d+',add,inputstr)

没有使用匹配匹配任何正则表达式为什么可以使用match.group()

2 个答案:

答案 0 :(得分:1)

正则表达式组在圆括号中:

re.sub('(\d+)',add,inputstr)

Regex101 Demo

答案 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'
相关问题