import re


def remove_optional_regex(pattern, name):
    """Removes an optional part of the regex by capture name

    Must be of the format '(?:[anything](?P<[name]>[anything])[anything])?'
    """
    return re.sub("\(\?:[^(]*\(\?P<{}>[^)]*\)[^)]*\)\?".format(name), "",
                  pattern)