>>> s = 'This and that.'
>>>> re.finditer(r'(th\w+) and (th\w+)',s,re.I).next().groups()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'callable_iterator' object has no attribute 'next'
报错原因是python版本的问题,修改办法如下:
>>> s = 'This and that.'
>>> res = re.finditer(r'(th\w+) and (th\w+)',str,re.I)#注意这个地方的版本问题
>>> print(next(res).group(2))
that
>>> res = re.finditer(r'(th\w+) and (th\w+)',str,re.I)#注意这个地方的版本问题
>>> print(next(res).groups())
('this', 'that')
>>> res = re.finditer(r'(th\w+) and (th\w+)',str,re.I)#注意这个地方的版本问题
>>> print(next(res).group(1))
this
>>> res = re.finditer(r'(th\w+) and (th\w+)',str,re.I)#注意这个地方的版本问题
>>> print(next(res).group(0))
this and that
补充:字符串前面加上 r 表示原生字符串(rawstring)
关注我,带你一起写bug
warning :未经授权,不得转载
有问题的小伙伴请在下方留言,喜欢就点个赞吧