定制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. 基于visual Studio2013解决C语言竞赛题之0502最小数替换

         题目

  2. northern truck 是什么牌子?具体_百度知道

    northern truck 是什么牌子?具体_百度知道 northern truck 是什么牌子?具体

  3. Windows多线程

    //简单的引出多线程是肿么回事儿....当点击下载的时候,下载内容还没结束也可以点击资源库,其实这就用了另一个线程,弹出“下载完成”对话框的时候,没有点击确定是不能点击主页面内容的,这就是用----- ...

  4. 简易视频播放器2 (基于Qt、opencv)

    因项目需要,需要实现一个对以保存的监测视频快速查看功能. 查询网上一些资料,初步简易的实现了一下. 实际效果图: 该程序基于Qt5.4,opencv248,开发环境为win8.1 结构为: video ...

  5. Online SVG to PNG/JPEG/TIFF conversion

    Online SVG to PNG/JPEG/TIFF conversion SVG to raster image conversion

  6. 安卓开发06:布局-线性布局 LinearLayout

    LinearLayout把视图组织成一行或一列.子视图能被安排成垂直的或水平的.线性布局是非常常用的一种布局方式. 请看一个布局例子: <LinearLayout xmlns:android=& ...

  7. [置顶] android 自定义TextView

    系统自带的控件TextView有时候没满一行就换行了,为了解决这个问题,自定义了一个TextView,只有一行显示不完全的情况下才会去换行显示,代码如下: package com.open.textv ...

  8. ListView 使用方法(Asp.Net)

    您将须要用到的独有数据绑定控件. Fritz Onion 代码下载位置: ExtremeASPNET2008_03.exe (192 KB) Browse the Code Online  文件夹 L ...

  9. codeforces #267 C George and Job(DP)

    职务地址:http://codeforces.com/contest/467/problem/C 太弱了..这题当时都没做出来..思路是有的,可是自己出的几组数组总是过不去..今天又又一次写了一遍.才 ...

  10. ADO.NET 2SqlDataAdapter、DataSet 的基本用法

    数据集完全独立于数据源,可以与数据源链接或者完全断开,其基本作用是为存储在内存缓存中的的数据提供关系视图 如果只是想读取和显示数据,则值需要使用数据读取器,尤其是处理大量数据的时候 如果需要处理数据, ...