定制pattern的string模板(template) 具体解释

本文地址: http://blog.csdn.net/caroline_wendy/article/details/28625179

string.Templatepattern是一个正則表達式, 能够通过覆盖pattern属性, 定义新的正則表達式.

如: 使用新的定界符"{{", 把{{var}}作为变量语法.

代码:

# -*- 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>\{\{) | # 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
)
''' t2 = MyTemplate('''
{{{{
{{var}}
''') print('MATCHES: ', t2.pattern.findall(t2.template))
print('SUBSTITUTED: ', t2.safe_substitute(var='replacement'))

输出:

    \$(?:
(?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: [('{{', '', '', ''), ('', 'var', '', '')]
SUBSTITUTED:
{{
replacement

Python - 定制pattern的string模板(template) 具体解释的更多相关文章

  1. python使用String的Template进行参数动态替换

    1.前言: 之前使用string的find(),从指定的param里面查找,是否包含了某个字符,有的话,使用replace进行替换,一个接口的param要替换的参数少的话,使用这种方式,的确可行,如果 ...

  2. Python 字符串操作及string模块使用

    python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串操作需求: python的字符串属性函数 python的string模块 1.字符串属性方法操作: 1.>字符串格式输出对 ...

  3. Python - 安全替换字符串模板(safe_substitute) 详细解释

    安全替换字符串模板(safe_substitute) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27057339 字 ...

  4. Magento的布局(Layout),块(Block)和模板(Template)

    public function indexAction() { //remove our previous echo //echo 'Hello Index!'; $this->loadLayo ...

  5. Python Flask学习笔记之模板

    Python Flask学习笔记之模板 Jinja2模板引擎 默认情况下,Flask在程序文件夹中的templates子文件夹中寻找模板.Flask提供的render_template函数把Jinja ...

  6. 微信小程序开发--模板(template)使用,数据加载,点击交互

    微信小程序视图层提供了 模板(template),可以在模板中定义代码片段,然后在不同的地方调用.结果在数据渲染那懵逼了.按照官网上对模板的说明和对数据的加载. 1.定义模板 使用name属性,作为模 ...

  7. C++ - 模板(template)中typename的使用方法

    声明template参数时, 前缀关键字class和typename可以互换; 使用关键字typename标识嵌套从属类型名称, 但不需在基类列表和成员初始化列表内使用. 从属名称(dependent ...

  8. 【template、import、include】微信小程序:模板(template)、引用(import、include)说明

    模板(template): 定义模板 <template name="[String]"> <!-- 模板代码 --> <view> <t ...

  9. django中将views.py中的python方法传递给html模板文件

    常规的模板渲染 from django.db import models # Create your models here. class ArticalType(models.Model): cap ...

随机推荐

  1. Java设计模式菜鸟系列(九)外观模式建模与实现

    转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39805735 外观模式(Facade):是为了解决类与类之间的依赖关系的,像spring一 ...

  2. 汉高澳大利亚sinox为什么不能下载源代码,因为sinox执行unix/linux/windows规划

    中国用户下载真正的澳大利亚sinox说完后sinox没有下载源代码. 这意味着,类似linux如下载linux 开源安装. 要知道.sinox并非linux. 首先,sinox是商业操作系统,就像 w ...

  3. (十六)JQuery Ready和angularJS controller的运行顺序问题

    项目中使用了JQuery和AngularJS框架,近期定位一个问题,原因就是JQuery Ready写在了angularJS controller之前,导致JQuery选择器无法选中须要的元素(由于a ...

  4. Ubuntu下安装和配置mysql

    一.检查 1.检查是否已经安装mysql whereis mysql 2.检查mysql服务是否已经启动 sudo netstat -tap | grep mysql 如果没有安装,下面就进行安装. ...

  5. 基于visual Studio2013解决C语言竞赛题之0805成绩输出

     题目

  6. NGUI 3.5教程(二)Label 标签 (Hello world)、多行文本

    写在前面:     本文将创建NGUI的第一个样例.依照编程传统,第一个样例,就是做一个Hello world 显示出来.NGUI.我们用Label来实现 . 欢迎大家纠错.拍砖!原创非常辛苦,如有转 ...

  7. jQuery选择器——全新的总结方式

    jQuery 选择器的总结 用于定位的选择器: 基本选择器:(用来进行绝对定位) $("#myELement")    选择id值等于myElement的元素,id值不能重复在文档 ...

  8. BZOJ 1342: [Baltic2007]Sound静音问题( 单调队列 )

    一开始写了个RMQ然后就T了... 好吧正解是单调队列, 维护两个单调队列... ----------------------------------------------------------- ...

  9. 基于visual Studio2013解决面试题之1408桶排序

     题目

  10. PHP $_SERVER

    $_SERVER 是一个包括了诸如头信息(header).路径(path).以及脚本位置(script locations)等等信息的数组.这个数组中的项目由 Web server创建.不能保证每一个 ...