Lets say I have this line:
\"My name is {name}\".format(name=\"qwerty\")
I know that the variable name is name
and so I can f
You can parse the format yourself with the string.Formatter() class to list all references:
from string import Formatter
names = [fn for _, fn, _, _ in Formatter().parse(yourstring) if fn is not None]
Demo:
>>> from string import Formatter
>>> yourstring = "My name is {myname}"
>>> [fn for _, fn, _, _ in Formatter().parse(yourstring) if fn is not None]
['myname']
You could subclass Formatter
to do something more fancy; the Formatter.get_field() method is called for each parsed field name, for example, so a subclass could work harder to find the right object to use.