def extract_substring(string, left, right, right_to_left=False):
    """Return a substring from a string.

    Parameters
    ----------
    string : |str|
        A string to be parsed.
    left : |str|
        A character representing the left bound of a target substring.
    right : |str|
        A character representing the right bound of a target substring.
    right_to_left : |bool|, optional
        Whether the `string` should be searched from right to left.

    Returns
    -------
    |str|
        The substring between the specified bounds.

    Examples
    --------
    >>> extract_substring('dup key : { "123" }', ':', '}')
    ' { "123" '

    >>> extract_substring('$_id_1', '$', '_', True)
    '_id'

    """
    if right_to_left:
        l_index = string.rfind(left) + len(left)
        r_index = string.rfind(right)
    else:
        l_index = string.find(left) + len(left)
        r_index = string.find(right)

    return string[l_index:r_index]