def eval_shift_distance(shift, reg_matches):
    """
    Compute the distance in characters a match has been shifted over.

    "reg_matches" is the set of regular matches as returned by find_regular_matches().

    The distance is defined as the number of characters between the shifted match
    and the closest regular match.
    """
    mid_matches = sorted(m for m in reg_matches
                         if (m[0] < shift[0] and m[1] > shift[1])
                         or (m[0] > shift[0] and m[1] < shift[1]))
    return (-(shift[0] - mid_matches[0][0])
            if mid_matches[0][0] < shift[0]
            else (mid_matches[-1][0] + len(mid_matches[-1][2])
                  - (shift[0] + len(shift[2]))))