常见的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编写屏幕截图程序方法
Feb 18 Python
Python基于pygame实现图片代替鼠标移动效果
Nov 11 Python
谈谈python中GUI的选择
Mar 01 Python
对python当中不在本路径的py文件的引用详解
Dec 15 Python
python 搭建简单的http server,可直接post文件的实例
Jan 03 Python
Python中一些深不见底的“坑”
Jun 12 Python
Golang GBK转UTF-8的例子
Aug 26 Python
Django后端发送小程序微信模板消息示例(服务通知)
Dec 17 Python
Python如何实现定时器功能
May 28 Python
基于tensorflow for循环 while循环案例
Jun 30 Python
Jupyter安装拓展nbextensions及解决官网下载慢的问题
Mar 03 Python
Jupyter Notebook内使用argparse报错的解决方案
Jun 03 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
PHP在线生成二维码代码(google api)
2013/06/03 PHP
php强大的时间转换函数strtotime
2016/02/18 PHP
Smarty分页实现方法完整实例
2016/05/11 PHP
ThinkPHP 模板引擎使用详解
2017/05/07 PHP
js 获取浏览器高度和宽度值(多浏览器)
2009/09/02 Javascript
8个超棒的学习 jQuery 的网站 推荐收藏
2011/04/02 Javascript
瀑布流布局代码一例
2014/04/11 Javascript
一个简单的jQuery插件ajaxfileupload.js实现ajax上传文件例子
2014/06/26 Javascript
学习JavaScript设计模式(继承)
2015/11/26 Javascript
Vue.js实现一个自定义分页组件vue-paginaiton
2016/09/05 Javascript
JS二分查找算法详解
2017/11/01 Javascript
基于Datatables跳转到指定页的简单实例
2017/11/09 Javascript
深入理解Vue父子组件生命周期执行顺序及钩子函数
2018/08/12 Javascript
vue中使用input[type=&quot;file&quot;]实现文件上传功能
2018/09/10 Javascript
从零开始用electron手撸一个截屏工具的示例代码
2018/10/10 Javascript
JS中实现一个下载进度条及播放进度条的代码
2019/06/10 Javascript
JS中的算法与数据结构之二叉查找树(Binary Sort Tree)实例详解
2019/08/16 Javascript
原生JavaScript实现拖动校验功能
2020/09/29 Javascript
更改Python命令行交互提示符的方法
2015/01/14 Python
Python的time模块中的常用方法整理
2015/06/18 Python
Django中redis的使用方法(包括安装、配置、启动)
2018/02/21 Python
django DRF图片路径问题的解决方法
2018/09/10 Python
利用pyinstaller打包exe文件的基本教程
2019/05/02 Python
scrapy-redis分布式爬虫的搭建过程(理论篇)
2020/09/29 Python
CSS中几个与换行有关的属性简明总结
2014/04/15 HTML / CSS
HTML5如何实现元素拖拽
2016/03/11 HTML / CSS
电影T恤、80年代T恤和80年代服装:TV Store Online
2020/01/05 全球购物
MATCHESFASHION澳大利亚/亚太地区:英国时尚奢侈品电商
2020/01/14 全球购物
成人高等教育毕业生自我鉴定
2013/10/22 职场文书
汽修专业学生自我鉴定
2013/11/16 职场文书
兰兰过桥教学反思
2014/02/08 职场文书
机电一体化毕业生自荐信
2014/06/19 职场文书
商务专员岗位职责范本
2014/06/29 职场文书
2015年度合同管理工作总结
2015/05/22 职场文书
贷款担保书范本
2015/09/22 职场文书
基于Redis结合SpringBoot的秒杀案例详解
2021/10/05 Redis