加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-16 23:32:18 所属栏目: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,4),(4,4)] 谁能告诉我,为什
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,4),(4,4)]

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

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

解决方法

>.*为零或更多,所以一旦消耗了四个字符,最后的零长度空字符串(不接触任何匹配的开始)仍然存在;和 >末尾的空字符串与模式不匹配 – 它不会在字符串的开头处开始.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读