String. The pattern of the template is a regular expression that can be defined by overriding pattern attributes and defining new regular expressions.
For example, using the new delimiter "{{", use {{var}} as the variable syntax.
Code:
#-*-Coding:utf-8-*-
'
Created
on 2014.6.5 @author: Administrator
@edition: Python 3.3.0, eclipse Pydev
' '
import string
t = string. Template (' $var ')
print (T.pattern.pattern)
class MyTemplate (string. Template):
delimiter = ' {{' pattern
= R ' '
(? p<escaped>\{\{) |
(? p<named>[_a-z][_a-z0-9]*) \}\} |
{(? p<braced>[_a-z][_a-z0-9]*)}\}\} |
(? p<invalid>)
' t2 = MyTemplate (' ' ' ' {{{{{{{}} '} '
)
print (' matches: ', T2.pattern.findall ( t2.template))
print (' substituted: ', T2.safe_substitute (var= ' replacement '))
Output:
\$(?:
(? p<escaped>\$) | # Escape sequence of two 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: [(' {], ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ' ')]
substituted:
{{
replacement
Author: csdn Blog spike_king
Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/