import re


def replace_mask(text):
    """Replaces multiple instances of mask with indexed versions.

    Parameters
    ----------
    text : string
        masked input, e.g. "This is a {mask} {mask} and {mask}"

    Returns
    -------
    string
        multiple instances of the same mask are replaced with indexed versions
        e.g. "This is a {mask[0]} {mask[1]} and {mask[2]}

    """
    mask_finder = re.compile(r'\{((?:[^\}]*:)?mask\d*)\}')
    i = 0
    while mask_finder.search(text):
        text = mask_finder.sub(r'{\1[%d]}' % i, text, 1)
        i += 1
    return text