def incrementMatching(text, matchSet, maxLength=None, n=2):
    """
    Adds incrementing n-digit numbers to text until it no longer matches any
    entries on matchSet.
    """
    if maxLength is not None and len(text) > maxLength:
        text = text[0:maxLength]
    while text in matchSet:
        if text[-n:].isdigit():
            num = int(text[-n:])
            num += 1
            num = str(num)
            while len(num) < n:
                num = "0" + num
            text = text[:-n]
        else:
            num = "0" * n
        if maxLength is not None and len(text) > maxLength-n:
            text = text[0:maxLength-n]
        text += num
    return text