Safe string formatting in Python
If you forbid function calls you cannot use getters which then is a big restriction. If you allow them it is hard to get it safe. Also your regexes will have collateral damages and filter legal expressions. So I think your approach is not so good. Don't allow users to mess with format strings. Use Template from string for that purpose.

to your regexes:


if you have a re for function you do not need one for system
[\w] includes [\d]
your re function catches only calls without arguments
re function and re system do not care about whitespaces and are vulnerable
all regexes do collateral damage when matching outside the format braces


here are some more test that currently fail

    '''
    some attacks
    &gt;&gt;&gt; safe_format('a_function(666)')
    ''

    &gt;&gt;&gt; safe_format('a_function ()')
    ''

    &gt;&gt;&gt; safe_format('{os. system("dir")}')
    ''

    some collateral damage
    &gt;&gt;&gt; safe_format('...system(left) {5}')
    '...system(left) 5'

    &gt;&gt;&gt; safe_format('f() gives {5}')
    'f(g) gives 5'

    '''