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
Post a Comment