How to find all placeholders for str.format in a python string using regex?

I am creating a class that renames a file using a user-specified format. This format will be a simple string, the str.format method will be called to fill in the blanks.

It turns out that my procedure will require retrieving the variable names contained in curly braces. For example, the string may contain {user} , which should give user . Of course, in one line there will be several sets of figures, and I will need to get the contents of each in the order in which they appear, and put them in a list.

Thus, "{foo}{bar}" should give ['foo', 'bar'] .

I suspect that the easiest way to do this is to use re.split , but I don't know anything about regular expressions. Can someone help me?

Thanks in advance!

+7
source share
2 answers

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'] 
+12
source

Another possibility is to use the actual Python Formatter to extract the field names for you:

 >>> import string >>> s = "{foo} spam eggs {bar}" >>> string.Formatter().parse(s) <formatteriterator object at 0x101d17b98> >>> list(string.Formatter().parse(s)) [('', 'foo', '', None), (' spam eggs ', 'bar', '', None)] >>> field_names = [name for text, name, spec, conv in string.Formatter().parse(s)] >>> field_names ['foo', 'bar'] 

or (shorter but less informative):

 >>> field_names = [v[1] for v in string.Formatter().parse(s)] >>> field_names ['foo', 'bar'] 
+37
source

All Articles