Python正则表达式使用经典实例


Posted in Python onJune 21, 2016

下面列出Python正则表达式的几种匹配用法,具体内容如下所示:

此外,关于正则的一切http://deerchao.net/tutorials/regex/regex.htm

1.测试正则表达式是否匹配字符串的全部或部分

regex=ur"" #正则表达式
if re.search(regex, subject):
do_something()
else:
do_anotherthing()

2.测试正则表达式是否匹配整个字符串

regex=ur"\Z" #正则表达式末尾以\Z结束
if re.match(regex, subject):
    do_something()
else:
    do_anotherthing()

3.创建一个匹配对象,然后通过该对象获得匹配细节(Create an object with details about how the regex matches (part of) a string)

regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
    # match start: match.start()
    # match end (exclusive): atch.end()
    # matched text: match.group()
    do_something()
else:
    do_anotherthing()

4.获取正则表达式所匹配的子串(Get the part of a string matched by the regex)

regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
    result = match.group()
else:
    result = ""

5. 获取捕获组所匹配的子串(Get the part of a string matched by a capturing group)

regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
    result = match.group(1)
else:
    result = ""

6. 获取有名组所匹配的子串(Get the part of a string matched by a named group)

regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
result = match.group"groupname")
else:
result = ""

7. 将字符串中所有匹配的子串放入数组中(Get an array of all regex matches in a string)

result = re.findall(regex, subject)

8.遍历所有匹配的子串(Iterate over all matches in a string)

for match in re.finditer(r"<(.*?)\s*.*?/\1>", subject)
    # match start: match.start()
    # match end (exclusive): atch.end()
    # matched text: match.group()

9.通过正则表达式字符串创建一个正则表达式对象(Create an object to use the same regex for many operations)

reobj = re.compile(regex)

10.用法1的正则表达式对象版本(use regex object for if/else branch whether (part of) a string can be matched)

reobj = re.compile(regex)
if reobj.search(subject):
    do_something()
else:
    do_anotherthing()

11.用法2的正则表达式对象版本(use regex object for if/else branch whether a string can be matched entirely)

reobj = re.compile(r"\Z") #正则表达式末尾以\Z 结束
if reobj.match(subject):
    do_something()
else:
    do_anotherthing()

12.创建一个正则表达式对象,然后通过该对象获得匹配细节(Create an object with details about how the regex object matches (part of) a string)

reobj = re.compile(regex)
match = reobj.search(subject)
if match:
    # match start: match.start()
    # match end (exclusive): atch.end()
    # matched text: match.group()
    do_something()
else:
    do_anotherthing()

13.用正则表达式对象获取匹配子串(Use regex object to get the part of a string matched by the regex)

reobj = re.compile(regex)
match = reobj.search(subject)
if match:
    result = match.group()
else:
    result = ""

14.用正则表达式对象获取捕获组所匹配的子串(Use regex object to get the part of a string matched by a capturing group)

reobj = re.compile(regex)
match = reobj.search(subject)
if match:
    result = match.group(1)
else:
    result = ""

15.用正则表达式对象获取有名组所匹配的子串(Use regex object to get the part of a string matched by a named group)

reobj = re.compile(regex)
match = reobj.search(subject)
if match:
    result = match.group("groupname")
else:
    result = ""

16.用正则表达式对象获取所有匹配子串并放入数组(Use regex object to get an array of all regex matches in a string)

reobj = re.compile(regex)
result = reobj.findall(subject)

17.通过正则表达式对象遍历所有匹配子串(Use regex object to iterate over all matches in a string)

reobj = re.compile(regex)
for match in reobj.finditer(subject):
    # match start: match.start()
    # match end (exclusive): match.end()
    # matched text: match.group()

字符串替换

1.替换所有匹配的子串

#用newstring替换subject中所有与正则表达式regex匹配的子串
result = re.sub(regex, newstring, subject)

2.替换所有匹配的子串(使用正则表达式对象)

reobj = re.compile(regex)
result = reobj.sub(newstring, subject)

字符串拆分

1.字符串拆分

result = re.split(regex, subject)

2.字符串拆分(使用正则表示式对象)

reobj = re.compile(regex)
result = reobj.split(subject)
Python 相关文章推荐
python用字典统计单词或汉字词个数示例
Apr 22 Python
Python调用C/C++动态链接库的方法详解
Jul 22 Python
跟老齐学Python之大话题小函数(2)
Oct 10 Python
python实现按行切分文本文件的方法
Apr 18 Python
详解Python多线程Selenium跨浏览器测试
Apr 01 Python
用Python实现读写锁的示例代码
Nov 05 Python
Python any()函数的使用方法
Oct 28 Python
通过celery异步处理一个查询任务的完整代码
Nov 19 Python
下载与当前Chrome对应的chromedriver.exe(用于python+selenium)
Jan 14 Python
Python函数基本使用原理详解
Mar 19 Python
python使用Word2Vec进行情感分析解析
Jul 31 Python
Python浮点型(float)运算结果不正确的解决方案
Sep 22 Python
常见的python正则用法实例讲解
Jun 21 #Python
实例讲解Python的函数闭包使用中应注意的问题
Jun 20 #Python
Python中的数学运算操作符使用进阶
Jun 20 #Python
Python中在for循环中嵌套使用if和else语句的技巧
Jun 20 #Python
解析Python中的生成器及其与迭代器的差异
Jun 20 #Python
Python判断列表是否已排序的各种方法及其性能分析
Jun 20 #Python
Python编程中装饰器的使用示例解析
Jun 20 #Python
You might like
10个实用的PHP代码片段
2011/09/02 PHP
整理php防注入和XSS攻击通用过滤
2015/09/13 PHP
PHP实现一维数组与二维数组去重功能示例
2018/05/24 PHP
jQuery实现原理的模拟代码 -6 代码下载
2010/08/16 Javascript
JavaScript实现生成GUID(全局统一标识符)
2014/09/05 Javascript
浅谈javascript的调试
2015/01/28 Javascript
浅析JavaScript动画
2015/06/10 Javascript
jQuery实现公告新闻自动滚屏效果实例代码
2016/07/14 Javascript
浅谈jQuery this和$(this)的区别及获取$(this)子元素对象的方法
2016/11/29 Javascript
BootStrap Tooltip插件源码解析
2016/12/27 Javascript
js学习总结之DOM2兼容处理重复问题的解决方法
2017/07/27 Javascript
从零开始用electron手撸一个截屏工具的示例代码
2018/10/10 Javascript
vue中过滤器filter的讲解
2019/01/21 Javascript
vue 父组件中调用子组件函数的方法
2019/06/06 Javascript
[03:09]显微镜下的DOTA2第一期——带你走进华丽的DOTA2世界
2014/06/20 DOTA
[06:07]刀塔密之二:攻之吾命受之吾幸
2014/07/03 DOTA
[00:35]DOTA2上海特级锦标赛 Newbee战队宣传片
2016/03/03 DOTA
[01:14:31]Secret vs VG 2018国际邀请赛淘汰赛BO3 第一场 8.23
2018/08/24 DOTA
[33:19]完美世界DOTA2联赛PWL S2 PXG vs InkIce 第一场 11.26
2020/11/30 DOTA
儿童学习python的一些小技巧
2018/05/27 Python
基于python实现雪花算法过程详解
2019/11/16 Python
Python爬虫爬取煎蛋网图片代码实例
2019/12/16 Python
python利用os模块编写文件复制功能——copy()函数用法
2020/07/13 Python
html5文本内容_动力节点Java学院整理
2017/07/11 HTML / CSS
美国职棒大联盟官方网上商店:MLBShop.com
2017/11/12 全球购物
Kangol帽子官网:坎戈尔袋鼠
2018/09/26 全球购物
英国领先的鞋类零售商:Shoe Zone
2018/12/13 全球购物
分公司经理岗位职责
2013/11/11 职场文书
物流毕业生个人的自我评价
2014/02/13 职场文书
大学生个人实习的自我评价
2014/02/15 职场文书
优秀公益广告词大全
2014/03/19 职场文书
2015年教学工作总结
2015/04/02 职场文书
离婚答辩状怎么写
2015/05/22 职场文书
2019年自助餐厅创业计划书模板
2019/08/22 职场文书
股东合作协议书模板2篇
2019/11/05 职场文书
基于tensorflow权重文件的解读
2021/05/26 Python