import re


def cut_section(start, end, s, position=0):
    """
    extract the piece of text between start pattern and end pattern starting at
    position <position>
    returns a tuple (subsection, end-position)

    """
    if start is None and end is None:
        return (s, 0)

    if start is not None:
        start_re = re.compile(start, re.DOTALL)
        start_m = start_re.search(s, position)
        if start_m:
            _s = start_m.start()
        else:
            _s = position
    else:
        _s = position
    if end is not None:
        end_re = re.compile(end, re.DOTALL)
        end_m = end_re.search(s, _s)
        if end_m:
            _e = end_m.end()
        else:
            _e = len(s)
    else:
        _e = len(s)
    result = (s[_s:_e], _e)
    return result