为什么这个正则表达式会打印 ('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)
()
最佳答案
来自有关的文档 groups
Return a tuple containing all the subgroups of the match, from 1 up to however many groups are in the pattern. The default argument is used for groups that did not participate in the match; it defaults to None.
在第一个正则表达式([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]+)
Above will find string composed of
a
orb
orc
and will store it in capturing group.
在第二个正则表达式 [abc]+
中,没有捕获组,因此显示空结果。
关于Python 正则表达式组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38216055/