常见的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()

18.字符串替换
 1).替换所有匹配的子串 

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

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

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

19.字符串拆分
 1).字符串拆分 

result = re.split(regex, subject)

2).字符串拆分(使用正则表示式对象)
reobj = re.compile(regex)
 result = reobj.split(subject)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python网络编程之文件下载实例分析
May 20 Python
使用Python写CUDA程序的方法
Mar 27 Python
python中os和sys模块的区别与常用方法总结
Nov 14 Python
Python程序退出方式小结
Dec 09 Python
Python字符串格式化%s%d%f详解
Feb 02 Python
python实现简易通讯录修改版
Mar 13 Python
Python多线程threading join和守护线程setDeamon原理详解
Mar 18 Python
Keras - GPU ID 和显存占用设定步骤
Jun 22 Python
Python3爬虫mitmproxy的安装步骤
Jul 29 Python
Python+unittest+requests+excel实现接口自动化测试框架
Dec 23 Python
python实战之90行代码写个猜数字游戏
Apr 22 Python
Python深度学习之Pytorch初步使用
May 20 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
12步入门Python中的decorator装饰器使用方法
Jun 20 #Python
You might like
声音就能俘获人心,蕾姆,是哪个漂亮小姐姐配音呢?
2020/03/03 日漫
Fatal error: Call to undefined function curl_init()解决方法
2010/04/09 PHP
用穿越火线快速入门php面向对象
2012/02/22 PHP
ThinkPHP访问不存在的模块跳转到404页面的方法
2014/06/19 PHP
php图像生成函数之间的区别分析
2012/12/06 Javascript
javascript右下角弹层及自动隐藏(自己编写)
2013/11/20 Javascript
Jquery实现自定义弹窗示例
2014/03/12 Javascript
JS+CSS实现带关闭按钮DIV弹出窗口的方法
2015/02/27 Javascript
JavaScript使用指针操作实现约瑟夫问题实例
2015/04/07 Javascript
javascript实时显示当天日期的方法
2015/05/20 Javascript
javascript的BOM汇总
2015/07/16 Javascript
js实现的早期滑动门菜单效果代码
2015/08/27 Javascript
javascript下拉列表中显示树形菜单的实现方法
2015/11/17 Javascript
jQuery1.9.1源码分析系列(十六)ajax之ajax框架
2015/12/04 Javascript
老生常谈遮罩层 滚动条的问题
2016/04/29 Javascript
Angular2 多级注入器详解及实例
2016/10/30 Javascript
Bootstrap php制作动态分页标签
2016/12/23 Javascript
Javascript(es2016) import和require用法和区别详解
2017/08/11 Javascript
JS/HTML5游戏常用算法之路径搜索算法 A*寻路算法完整实例
2018/12/14 Javascript
详解Nodejs get获取远程服务器接口数据
2019/03/26 NodeJs
更改Python命令行交互提示符的方法
2015/01/14 Python
python去掉空白行的多种实现代码
2018/03/19 Python
python基于http下载视频或音频
2018/06/20 Python
详解Django定时任务模块设计与实践
2019/07/24 Python
在pandas中遍历DataFrame行的实现方法
2019/10/23 Python
Python SSL证书验证问题解决方案
2020/01/13 Python
Python如何进行时间处理
2020/08/06 Python
python解包用法详解
2021/02/17 Python
CSS3中颜色线性渐变实战
2015/07/18 HTML / CSS
GUESS德国官网:美国牛仔服装品牌
2017/02/14 全球购物
毕业生简单求职信
2013/11/19 职场文书
教师反邪教心得体会
2016/01/15 职场文书
工作建议书范文
2019/07/08 职场文书
JavaScript 实现页面滚动动画
2021/04/24 Javascript
如何使JavaScript休眠或等待
2021/04/27 Javascript
Django分页器的用法你都了解吗
2021/05/26 Python