import re


def matching_segment_range(segments, regexp, invert=False):
    """Returns a range object that yields the indices of those segments that
    match 'regexp' from all segments that are returned by function
    'segment_data'.

    Args:
        segments (list): List of segments as returned by function
            'segment_data'.
        regexp (string or regex object): the regular expression object or
            string that has been used to divide the data into segments with
            function 'segment_data'.
        invert(boolean): If True, return the range of segments that do not
            match regexp

    Returns:
        range object. Indices of the matching segments.
    """
    if isinstance(regexp, str):
        regexp = re.compile(regexp)
    if invert:
        return range(1 if regexp.match(segments[0]) else 0, len(segments), 2)
    else:
        return range(0 if regexp.match(segments[0]) else 1, len(segments), 2)