(pattern)
return result
r"""
    convert a format string with %s and %d to a regex

    %s => .*
    %d => [0-9]+
    %% => %

    everything else gets `re.escape`d

    >>> import re
    >>> format_ = '%shello %%sam %s you are %d years old.'
    >>> regex = _format_to_regex(format_)
    >>> print(regex)
    .*hello\ %sam\ .*\ you\ are\ [0-9]+\ years\ old\.
    >>> bool(re.match(regex, format_ % ("Oh ", "i am", 6)))
    True
    >>> bool(re.match(regex, format_))
    False
r"""
    convert a format string with %s and %d to a regex
r convert a format string with %s and %d to a regex
def _format_to_regex(pattern):
    r"""
    convert a format string with %s and %d to a regex

    %s => .*
    %d => [0-9]+
    %% => %

    everything else gets `re.escape`d

    >>> import re
    >>> format_ = '%shello %%sam %s you are %d years old.'
    >>> regex = _format_to_regex(format_)
    >>> print(regex)
    .*hello\ %sam\ .*\ you\ are\ [0-9]+\ years\ old\.
    >>> bool(re.match(regex, format_ % ("Oh ", "i am", 6)))
    True
    >>> bool(re.match(regex, format_))
    False

    """
    formatting_re = r'%[%sd]'
    parts = [re.escape(part) for part in re.split(formatting_re, pattern)]
    replacements = [{'%s': '.*', '%d': '[0-9]+', '%%': '%'}[r]
                    for r in re.findall(formatting_re, pattern)]
    result = parts.pop(0)
    for p, r in zip(parts, replacements):
        result += r + p

    return result
def _format_to_regex ( pattern ) : formatting_re = r'%[%sd]' parts = [ re . escape ( part ) for part in re . split ( formatting_re , pattern ) ] replacements = [ { '%s' : '.*' , '%d' : '[0-9]+' , '%%' : '%' } [ r ] for r in re . findall ( formatting_re , pattern ) ] result = parts . pop ( 0 ) for p , r in zip ( parts , replacements ) : result += r + p return result