import uuid


def _replace_substrings(code, first_char, last_char):
    """Replace the substrings between first_char and last_char
    with a unique id.
    Return the replaced string and a list of replacement
    tuples: (unique_id, original_substring)
    """
    substrings = []
    level = 0
    start = -1
    for i in range(len(code)):
        if code[i] == first_char:
            level += 1
            if level == 1:
                start = i
        if code[i] == last_char:
            level -= 1
            if level == 0:
                substrings.append((start, i + 1))
    code_ = code
    replacements = []
    for substr_index in substrings:
        unique_id = '{}"{}"{}'.format(first_char, str(uuid.uuid4()), last_char)
        substring = code[substr_index[0]:substr_index[1]]
        code_ = code_.replace(substring, unique_id)
        replacements.append((unique_id, substring))
    return code_, replacements