python里修改string.Template的正则表达式
来源:互联网 发布:win7一键网络共享工具 编辑:程序博客网 时间:2024/06/08 01:27
通过下面的例子可以查看到string.Template的正则表达式:
import stringt = string.Template('$var')print(t.pattern.pattern)
结果输出如下:
\$(?:
(?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
)
这个就是默认的正则表达式。
其实可以自己手动修改它,如下面的例子:
import reimport stringclass MyTemplate(string.Template): delimiter = '{{' pattern = r''' \{\{(?: (?P<escaped>\{\{)| (?P<named>[_a-z][_a-z0-9]*)\}\}| (?P<braced>[_a-z][_a-z0-9]*)\}\}| (?P<invalid>) ) '''t = MyTemplate('''{{{{{{var}}''')print('MATCHES:', t.pattern.findall(t.template))print('SUBSTITUTED:', t.safe_substitute(var='replacement'))
在这个例子里,修改正则表达式增加{{ }}的识别。结果输出如下:
MATCHES: [('{{', '', '', ''), ('', 'var', '', '')]
SUBSTITUTED:
{{
replacement
Python游戏开发入门
http://edu.csdn.net/course/detail/5690
你也能动手修改C编译器
http://edu.csdn.net/course/detail/5582
纸牌游戏开发
http://edu.csdn.net/course/detail/5538
五子棋游戏开发
http://edu.csdn.net/course/detail/5487
RPG游戏从入门到精通
http://edu.csdn.net/course/detail/5246
WiX安装工具的使用
http://edu.csdn.net/course/detail/5207
俄罗斯方块游戏开发
http://edu.csdn.net/course/detail/5110
boost库入门基础
http://edu.csdn.net/course/detail/5029
Arduino入门基础
http://edu.csdn.net/course/detail/4931
Unity5.x游戏基础入门
http://edu.csdn.net/course/detail/4810
TensorFlow API攻略
http://edu.csdn.net/course/detail/4495
TensorFlow入门基本教程
http://edu.csdn.net/course/detail/4369
C++标准模板库从入门到精通
http://edu.csdn.net/course/detail/3324
跟老菜鸟学C++
http://edu.csdn.net/course/detail/2901
跟老菜鸟学python
http://edu.csdn.net/course/detail/2592
在VC2015里学会使用tinyxml库
http://edu.csdn.net/course/detail/2590
在Windows下SVN的版本管理与实战
http://edu.csdn.net/course/detail/2579
Visual Studio 2015开发C++程序的基本使用
http://edu.csdn.net/course/detail/2570
在VC2015里使用protobuf协议
http://edu.csdn.net/course/detail/2582
在VC2015里学会使用MySQL数据库
http://edu.csdn.net/course/detail/2672
阅读全文
0 0
- python里修改string.Template的正则表达式
- python里使用string.Template实现自定义转义字符和正则表达式替换
- python里使用正则表达式
- 浅析python的string.Template
- 正则表达式里的?:
- python里使用正则表达式的转义字符的匹配
- string的正则表达式
- python里使用正则表达式的重复模式
- python里使用正则表达式的非贪婪模式
- python里使用正则表达式的集合字符
- python里使用正则表达式的连接符
- python里使用正则表达式的句号和星号
- python里使用正则表达式的预先定义集合字符
- python里使用正则表达式的边界符进行匹配
- python里使用正则表达式的限制边界功能
- python里使用正则表达式的全匹配功能
- python里使用正则表达式的词组匹配功能
- python里使用正则表达式的分组命名方式
- 各国上班族要工作多少天才能买得起iPhone?
- 缓存数组swift2.3
- select选择下拉框
- java单例模式
- IIS7、IIS8支持html包含页
- python里修改string.Template的正则表达式
- 树状数组求逆序数
- Java并发编程实战(1)
- POJ -1852 Ants(白书1.6.2)
- Kotlin 在studio的简单配置
- java笔记汇总整理网址
- Avro,Thrift,Gb 三种协议
- caffe学习
- 诗歌四 滕王阁序