How can I find all placeholders for str.format in a python string using a regex? -


i'm creating class renames file using user-specified format. format simple string str.format method called fill in blanks.

it turns out procedure require extracting variable names contained in braces. example, string may contain {user}, should yield user. of course, there several sets of braces in single string, , i'll need contents of each, in order in appear , output them list.

thus, "{foo}{bar}" should yield ['foo', 'bar'].

i suspect easiest way use re.split, know nothing regular expressions. can me out?

thanks in advance!

using re.findall():

in [5]: import re  in [8]: strs="{foo} spam eggs {bar}"  in [9]: re.findall(r"{(\w+)}",strs) out[9]: ['foo', 'bar'] 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -