Yields contiguous MatchObjects of r in s.
    Raises ValueError if r eventually doesn't match contiguously.

def matchiter(r, s, flags=0):
    """
    Yields contiguous MatchObjects of r in s.
    Raises ValueError if r eventually doesn't match contiguously.
    """
    if isinstance(r, basestring):
        r = re.compile(r, flags)
    i = 0
    while s:
        m = r.match(s)
        g = m and m.group(0)
        if not m or not g:
            raise ValueError("{}: {!r}".format(i, s[:50]))
        i += len(g)
        s = s[len(g):]
        yield m