python里使用正则表达式的内嵌功能选项标志
2023-09-14 09:10:35 时间
在前面学习过正则表达式有一些标志,可以通过编译时添加进去。比如学习过的re.DOTALL标志,这些标志都是在编译设置的,如果不是立即编译正则表达式时,就不能使用这些参数。比如你需要把一个正则表达式当作参数传送给别的函数时,就比较麻烦,不仅要传送正则表达式,还需要传送选项标志。如果开发人员不小心,把一个参数搞丢了,或者搞错了,就容易出错,这时怎么办呢?显然有更好的方式,就是把选项参数融入正则表达式里,这样就达到原子一致性,要丢就一起丢,不会出现半桶水的样子。如下面例子:
结果输出如下:
Text : This is some text -- with punctuation.
Pattern : \bT\w+
Matches : ['This']
(?i):
Text : This is some text -- with punctuation.
Pattern : (?i)\bT\w+
标志 对应的字母
ASCII a
IGNORECASE i
MULTILINE m
DOTALL s
深入浅出Numpy
五子棋游戏开发
http://edu.csdn.net/course/detail/5487
#python 3.6
#蔡军生
#http://blog.csdn.net/caimouse/article/details/51749579
#
import re
text = 'This is some text -- with punctuation.'
pattern = r'\bT\w+'
regex = re.compile(pattern)
print('Text :', text)
print('Pattern :', pattern)
print('Matches :', regex.findall(text))
print('\n(?i):')
pattern = r'(?i)\bT\w+'
regex = re.compile(pattern)
print('Text :', text)
print('Pattern :', pattern)
print('Matches :', regex.findall(text))
结果输出如下:
Text : This is some text -- with punctuation.
Pattern : \bT\w+
Matches : ['This']
(?i):
Text : This is some text -- with punctuation.
Pattern : (?i)\bT\w+
Matches : ['This', 'text']
在这个例子里,就是添加(?i)在正则表达式里面表示不对大小字母敏感。
标志 对应的字母
ASCII a
IGNORECASE i
MULTILINE m
DOTALL s
VERBOSE x
深入浅出Numpy
http://edu.csdn.net/course/detail/6149
Python游戏开发入门
你也能动手修改C编译器
纸牌游戏开发
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
相关文章
- 【说站】Python脚本如何指定文件
- 【说站】python中pdb的启用
- 【说站】python正则表达式问号的使用
- 【说站】python Decimal解决计算问题
- python的特点和优势_Java与Python异同
- Python抛出异常_python抛出异常的作用
- python lambda表达式 if_Python学习-lambda表达式
- Python字典提取_python字典键对应的值
- 廖雪峰的 Python 教程_python基础教程廖雪pdf
- python 列表 【基本使用功能】
- Python操作系统
- python-Django-Django 常用模板标签和过滤器(一)
- python通过正则表达式分析网页中的图片并进行替换详解编程语言
- python使用Queue实现优先级队列详解编程语言
- python 正则表达式分析nginx日志
- 用Python仿写MSSQL 编程体验更有趣(python仿mssql)
- Python编程连接MySQL:从零开始(python与mysql)
- Python中使用装饰器和元编程实现结构体类实例
- 跟老齐学Python之有点简约的元组