UNIQ = '_UNIQUE_STRING_'


class MarkPlaceholders(dict):

    def __getitem__(self, key):
        return UNIQ + '(?P<%s>.*?)' % key + UNIQ

def format_to_re(format):
    parts = (format % MarkPlaceholders()).split(UNIQ)
for i in range(0, len(parts), 2):
    parts[i] = re.escape(parts[i])
return ''.join(parts)
Python: Convert format string to regular expression