Two things need to be explained here: the behavior of quantitative groups and the design of the findall() method.
In the first example, [abc] matches a , which is fixed in group # 1. Then it matches b and fixes it in group # 1, overwriting a . Then again with c , and what remains in group # 1 at the end of the match.
But it matches the whole line. If you used search() or finditer() , you could look at 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 common matches; if there are groups, the list contains all captures, but not a general match.
Thus, both of your regular expressions correspond to the whole line, but the first also captures and discards each character individually (which is useless). This is just the unexpected behavior of findall() , which makes it look like you get different results.
Alan moore
source share