您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

为什么Python findall()和finditer()在unanchored.*搜索中返回空匹配?

5b51 2022/1/14 8:23:06 python 字数 1020 阅读 574 来源 www.jb51.cc/python

findall()和finditer()的 Python文档声明: Empty matches are included in the result unless they touch the beginning of another match 这可以证明如下: In [20]: [m.span() for m in re.finditer('.*', 'test')] Out[20]: [(0

概述

Empty matches are included in the result unless they touch the
beginning of another match

这可以证明如下:

In [20]: [m.span() for m in re.finditer('.*','test')]
Out[20]: [(0,4),(4,4)]

谁能告诉我,为什么这个模式首先会返回一个空的匹配?不应该.*消耗整个字符串并返回一个匹配?而且,如果我将模式锚定到字符串的开头,为什么最后没有空匹配?例如

In [22]: [m.span() for m in re.finditer('^.*','test')]
Out[22]: [(0,4)]

总结

以上是编程之家为你收集整理的为什么Python findall()和finditer()在unanchored.*搜索中返回空匹配?全部内容,希望文章能够帮你解决为什么Python findall()和finditer()在unanchored.*搜索中返回空匹配?所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶