> 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 :-)

 >>> r = re.compile(r'(?<!%)(?:%%)*%\(([^)]*)\)')

works on all but your pathological '%(and so is this)%()%%' and 
if any programmer working for me used that as their formatting 
variable, they'd either be out of a job, or I would be for hiring 
such a psychopath. :)

 >>> print r.sub(lambda x: '@@@%s@@@' % x.group(0), s)
@@@%(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)@@@%.
 >>> keys = r.findall(s)
 >>> keys
['key', 'but this is', '%(and so is this', '', '', 'new\nlines', 
'percentages']

The extra empty item (keys[3]) is from that pathological case. 
Otherwise, it seems to do the trick.

-tkc
ps:  you're a cruel, cruel fellow for throwing out such a 
mind-bender this early in my morning. :)  Thanks for the fun!


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to