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两种遍历字典(dict)的方法比较
May 29 Python
Python 获取新浪微博的最新公共微博实例分享
Jul 03 Python
如何使用VSCode愉快的写Python于调试配置步骤
Apr 06 Python
Python实现按当前日期(年、月、日)创建多级目录的方法
Apr 26 Python
分享一下Python数据分析常用的8款工具
Apr 29 Python
解决已经安装requests,却依然提示No module named requests问题
May 18 Python
win7 x64系统中安装Scrapy的方法
Nov 18 Python
Django基础知识 web框架的本质详解
Jul 18 Python
python thrift 实现 单端口多服务的过程
Jun 08 Python
django form和field具体方法和属性说明
Jul 09 Python
Python中的min及返回最小值索引的操作
May 10 Python
如何利用Python实现一个论文降重工具
Jul 09 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
一个简单的php实现的MySQL数据浏览器
2007/03/11 PHP
生成随机字符串和验证码的类的PHP实例
2013/12/24 PHP
php调用C代码的实现方法
2014/03/11 PHP
php中动态调用函数的方法
2015/03/16 PHP
PHP文字转图片功能原理与实现方法分析
2017/08/31 PHP
无缝滚动改进版支持上下左右滚动(封装成函数)
2012/12/04 Javascript
jquery自定义表格样式
2015/11/23 Javascript
纯js代码制作的网页时钟特效【附实例】
2016/03/30 Javascript
弹出遮罩层后禁止滚动效果【实现代码】
2016/04/29 Javascript
JQuery validate插件Remote用法大全
2016/05/15 Javascript
jQuery Mobile动态刷新页面样式的实现方法
2016/05/28 Javascript
bootstrap 模态框(modal)实现水平垂直居中显示
2017/01/23 Javascript
webstorm中vue语法的支持详解
2018/05/09 Javascript
jQuery实现的简单手风琴效果示例
2018/08/29 jQuery
JavaScript类型相关的常用操作总结
2019/02/14 Javascript
详解Vue中使用Axios拦截器
2019/04/22 Javascript
对TypeScript库进行单元测试的方法
2019/07/18 Javascript
js实现打字小游戏
2019/12/17 Javascript
Nodejs文件上传、监听上传进度的代码
2020/03/27 NodeJs
浅谈vue中$event理解和框架中在包含默认值外传参
2020/08/07 Javascript
解决VUE项目localhost端口服务器拒绝连接,只能用127.0.0.1的问题
2020/08/14 Javascript
Python tkinter实现的图片移动碰撞动画效果【附源码下载】
2018/01/04 Python
python 识别图片中的文字信息方法
2018/05/10 Python
解决Mac安装scrapy失败的问题
2018/06/13 Python
pygame实现雷电游戏雏形开发
2018/11/20 Python
python 生成任意形状的凸包图代码
2020/04/16 Python
CSS伪类与CSS伪元素的区别及由来具体说明
2012/12/07 HTML / CSS
农村党支部先进事迹
2014/01/14 职场文书
业务总经理岗位职责
2014/02/03 职场文书
银行职员自我鉴定
2014/04/20 职场文书
2014年预备党员端正入党动机思想汇报
2014/09/13 职场文书
借款民事起诉状范文
2015/05/19 职场文书
低端且暴利的线上线下创业项目分享
2019/09/03 职场文书
关于Python OS模块常用文件/目录函数详解
2021/07/01 Python
详解overflow:hidden的作用(溢出隐藏、清除浮动、解决外边距塌陷)
2021/07/01 HTML / CSS
Python 中面向接口编程
2022/05/20 Python