Custom pattern string Templates (template) for specific explanations
This address: http://blog.csdn.net/caroline_wendy/article/details/28625179
string. Template pattern is a regular form that enables you to define a new regular form by overriding the Pattern property .
For example: Use the new delimiter "{{" and {{var}} as the variable syntax.
Code:
#-*-Coding:utf-8-*-' Created on 2014.6.5@author:administrator@edition:python 3.3.0, Eclipse pydev ' import stringt = String. Template (' $var ') print (T.pattern.pattern) class MyTemplate (string. Template): delimiter = ' {' pattern = R ' ' \{\{(?: (? p<escaped>\{\{) | # Escape sequence of delimiters (?
p<named>[_a-z][_a-z0-9]*) \}\} | # delimiter and a Python identifier {(? p<braced>[_a-z][_a-z0-9]*)}\}\} | # delimiter and a braced identifier (? p<invalid>) # other ill-formed delimiter Exprs ) ' t2 = MyTemplate (' ' {{{{{{{{ }}} ') Print (' MATCHES: ', T2.pattern.findall (t2.template)) print (' substituted: ', T2.safe_substitute (var= ' replacement '))
Output:
\$(?: (? p<escaped>\$) | # Escape sequence of delimiters (? p<named>[_a-z][_a-z0-9]*) | # delimiter and a Python identifier {(? p<braced>[_a-z][_a-z0-9]*)} | # delimiter and a braced identifier (? p<invalid>) # other ill-formed delimiter Exprs ) MATCHES: [(' {' {', ' ', ' ', ' '), (', ' var ', ', ")]substituted: {{replacement
Python-Custom pattern string template for specific explanations