How do capture groups work? (wrt python regular expressions)
Question: While using regex to help solve a problem in the [Python
Challenge](http://www.pythonchallenge.com/), I came across some behaviour that
confused me.

from [here](http://docs.python.org/library/re.html#regular-expression-syntax):

(...) Matches whatever regular expression is inside the parentheses.

and

'+' Causes the resulting RE to match 1 or more repetitions of the preceding
RE.

So this makes sense:

    
    
    >>>import re
    >>>re.findall(r"(\d+)", "1111112")
    ['1111112']
    

But this doesn't:

    
    
    >>> re.findall(r"(\d)+", "1111112")
    ['2']
    

I realise that findall returns only groups when groups are present in the
regex, but why is only the '2' returned? What happends to all the 1's in the
match?


Answer: Because you only have one capturing group, but it's "run" repeatedly, the new
matches are repeatedly entered into the "storage space" for that group. In
other words, the `1`s were lost when they were "overwritten" by subsequent
`1`s and eventually the `2`.

