def expand_abbreviations(txt, fields):
def _expand(matchobj):<EOL><INDENT>s = matchobj.group("<STR_LIT>")<EOL>if s not in fields:<EOL><INDENT>matches = [x for x in fields if x.startswith(s)]<EOL>if len(matches) == <NUM_LIT:1>:<EOL><INDENT>s = matches[<NUM_LIT:0>]<EOL><DEDENT><DEDENT>return "<STR_LIT>" % s<EOL><DEDENT>return re.sub(FORMAT_VAR_REGEX, _expand, txt)<EOL>
Expand abbreviations in a format string.

    If an abbreviation does not match a field, or matches multiple fields, it
    is left unchanged.

    Example:

        >>> fields = ("hey", "there", "dude")
        >>> expand_abbreviations("hello {d}", fields)
        'hello dude'

    Args:
        txt (str): Format string.
        fields (list of str): Fields to expand to.

    Returns:
        Expanded string.