为什么此正则表达式会打印('c',)
和()
?我想"([abc])+" === "([abc])([abc])([abc])..."
>>> import re
>>> m = re.match("([abc])+", "abc")
>>> print m.groups()
('c',)
>>> m.groups(0)
('c',)
>>> m = re.match("[abc]+", "abc")
>>> m.groups()
()
>>> m.groups(0)
()
从有关组的文档中
返回一个包含匹配项所有子组的元组,从1到模式中的许多组。默认参数用于未参加比赛的组;它默认为无。
在第一个正则表达式中([abc])+
,它是匹配字符a
或b
或,c
但将仅存储最后一个匹配项
([abc])+
<----->
Matches a or b or c
Observe carefully. Capturing groups are surrounding only the character class
So, only one character from the matched character class can be stored in capturing group.
如果要abc
在捕获组中捕获字符串,请使用
([abc]+)
上面将找到由
a
或b
或组成的字符串c
,并将其存储在捕获组中。
在第二个正则表达式中[abc]+
,没有捕获组,因此显示为空结果。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句