extracting string.Template substitution placeholders

gmflanagan cyclebelfast at gmail.com
Fri Jan 17 01:07:07 EST 2014


On Sunday, January 12, 2014 3:08:31 PM UTC, Eric S. Johansson wrote:
> As part of speech recognition accessibility tools that I'm building, I'm 
> 
> using string.Template. In order to construct on-the-fly grammar, I need 
> 
> to know all of the identifiers before the template is filled in. what is 
> 
> the best way to do this?
> 

Try this:

import string
cmplxstr="""a simple $string a longer $string a $last line ${another} one"""

def finditer(s):
    for match in string.Template.pattern.finditer(s):
        arg = match.group('braced') or match.group('named')
        if arg:
            yield arg


if __name__ == '__main__':
    print set(finditer(cmplxstr))




More information about the Python-list mailing list