Re: [问题] 正规表示法"*?"

楼主: canamvskid (覚醒図鉴)   2016-06-06 02:26:03
: 这个其实更有趣了,单看 o*? , 代表的是 零个以上的 o 尽量少取
: 根据 re.findall 的特性,他会由左而右不重叠的取出来
: 如果以 o*? 来去对 fooood 做 findall ,我们由左而右来看,
: 黄色的就是目前正在检查的:
: fooood , f 符合 "零个以上的 o" 吗?
: => Yes, "零个 o" , 所以得到第一个 ''
: fooood , o 符合 "零个以上的 o" 吗?
: => Yes, 符合零个 o , 因为是 "尽量少取"
: 所以得到第二个 ''
: fooood , o 符合 "零个以上的 o" 吗?
: => Yes, 符合零个 o , 因为是 "尽量少取"
: 所以得到第三个 ''
: 依此类推,所以你会得到很多的个 ''
想请问一下,我用s1 = 'abc',然后 re.findall(r'o*?', s1)
得到的结果是 ['', '', '', '']
如果是从左到右一个个比对的话不是只有3个吗?
为什么最后会多一个出来呢?
作者: rlinlen (Lin_len)   2016-06-06 11:33:00
'' 属于'abc'的子集,findall的*(match 0)就把他抓进来了试试看r'.*?'和r'.+?'会比较好懂
作者: LiloHuang (十年一刻)   2016-06-08 21:05:00
每个字符之前都会 match,包含字串结尾 $ 前面的位置Empty matches are included in the result unless theresult unless they touch the beginning of anothermatch. https://goo.gl/qcxoYj有兴趣也可以玩一下 re.findall(r'o*?', s1, re.DEBUG)抓到的空白为标问号的位置 '?a?b?c?' 这样应该比较清楚就像 re.findall(r'o*?', '') 回传的不是 empty list更正 空字串 不是空白 XD

Links booklink

Contact Us: admin [ a t ] ucptt.com