There are two things that need to be explained here: the behavior of quantified groups, and the design of the findall()
method.
In your first example, [abc]
matches the a
, which is captured in group #1. Then it matches b
and captures it in group #1, overwriting the a
. Then again with the c
, and that's what's left in group #1 at the end of the match.
But it does match the whole string. If you were using search()
or finditer()
, you would be able to look at the MatchObject and see that group(0)
contains abc
and group(1)
contains c
. But findall()
returns strings, not MatchObjects. If there are no groups, it returns a list of the overall matches; if there are groups, the list contains all the captures, but not the overall match.
So both of your regexes are matching the whole string, but the first one is also capturing and discarding each character individually (which is kinda pointless). It's only the unexpected behavior of findall()
that makes it look like you're getting different results.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…