Finding first N occurrences of regex in Python
Question: So this should be easy but I somehow miss the answer on SO or Python docs. I
am using this code:

    
    
    myregex.findall(source)
    

This produces all matches of myregex as a list. Now, the problem is that
source is long and I only need first 6 occurrences of substring matching
myregex. I imagine that it would be much faster if matching process could stop
after finding first n occurrences. How do I do something like:

    
    
    myregex.findall(source, n)
    

?


Answer: Use [`re.finditer`](http://docs.python.org/2/library/re.html#re.finditer):

    
    
    import itertools
    for m in itertools.islice(re.finditer(pat, text), 6):
        ...
    

`re.finditer` is a generator that produces `match` objects on demand. You can
get the complete match from `m.group(0)`, or individual pattern matches from
`m.group(1)` and up.

