Trivial string substitution/parser
Graham Breed
x31equsenet at gmail.com
Tue Jun 19 09:02:39 EDT 2007
Duncan Booth wote:
> If you must insist on using backslash escapes (which introduces the
> question of how you get backslashes into the output: do they have to be
> escaped as well?) then use string.Template with a custom pattern.
If anybody wants this, I worked out the following regular expression
which seems to work:
(?P<escaped>\\)\$ | # backslash escape pattern
\$(?:
(?P<named>[_a-z][_a-z0-9]*) | # delimiter and Python identifier
{(?P<braced>[_a-z][_a-z0-9]*)} | # delimiter and braced identifier
(?P<invalid>) # Other ill-formed delimiter exprs
)
The clue is string.Template.pattern.pattern
So you compile that with verbose and case-insensitive flags and set it
to "pattern" in a string.Template subclass. (In fact you don't have
to compile it, but that behaviour's undocumented.) Something like
>>> regexp = """
... (?P<escaped>\\\\)\\$ | # backslash escape pattern
... \$(?:
... (?P<named>[_a-z][_a-z0-9]*) | # delimiter and identifier
... {(?P<braced>[_a-z][_a-z0-9]*)} | # ... and braced identifier
... (?P<invalid>) # Other ill-formed delimiter exprs
... )
... """
>>> class BackslashEscape(Template):
... pattern = re.compile(regexp, re.I | re.X)
...
Graham
More information about the Python-list
mailing list