how to get all the "variables" of a string formating?

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Thu Dec 7 06:27:38 EST 2006


On Wed, 06 Dec 2006 10:58:56 -0800, dakman at gmail.com wrote:

> "Is there an easy (i.e.: no regex) way to do get the names of all
> parameters? "
> 
> ...regexp is the easy way :D

I'd like to see this regex. And make sure it works correctly with this
format string:

"""%(key)s
%%(this is not a key)d
%%%(but this is)f 
%%%%%%%(%(and so is this)%()%%)u 
and don't forget the empty case %()c
but not %%%%%%()E
and remember to handle %(new
lines)X correctly
and %(percentages)%."""

It should list the keys as:

'key'
'but this is'
'%(and so is this)%()%%' 
''
'new\nlines'
'percentages'


I love easy regexes :-)



-- 
Steven.




More information about the Python-list mailing list