0

I'd like to transform a string expecting a dict of parameters to the list of keys of the expected dict, e.g. find f such that:

f("some text %(foo)s %(bar)s") == ['foo', 'bar',] # True

Is there some way to do it ?

1

2 Answers 2

1

Smth like

>>> import re
>>> re.findall("%\(([^\)]+)\)[sif]", "some text %(foo)s %(bar)s", re.M)
['foo', 'bar']

[sif] part can be extended with symbols from table on http://docs.python.org/library/stdtypes.html#string-formatting-operations

Sign up to request clarification or add additional context in comments.

Comments

0

How about this:

>>> S = "some text %(foo)s %(bar)s"
>>> print re.findall(r'%\((.*?)\)', S)
['foo', 'bar']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.