Why does this regex print
"([abc])+" === "([abc])([abc])([abc])..."
>>> import re
>>> m = re.match("([abc])+", "abc")
>>> print m.groups()
>>> m = re.match("[abc]+", "abc")
From documentation about 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.
In the first regex
([abc])+, it is matching character
c but will store only the last match
([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.
If you want to capture string
abc in a capturing group use
Above will find string composed of
cand will store it in capturing group.
In second regex
[abc]+, there are no capturing groups, so an empty result is shown.