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 03 Python
Python中的startswith和endswith函数使用实例
Aug 25 Python
Python实现抓取HTML网页并以PDF文件形式保存的方法
May 08 Python
Python基于Floyd算法求解最短路径距离问题实例详解
May 16 Python
使用python将图片按标签分入不同文件夹的方法
Dec 08 Python
Django框架中间件(Middleware)用法实例分析
May 24 Python
使用python实现简单五子棋游戏
Jun 18 Python
基于python-opencv3的图像显示和保存操作
Jun 27 Python
Python异常继承关系和自定义异常实现代码实例
Feb 20 Python
PyTorch之nn.ReLU与F.ReLU的区别介绍
Jun 27 Python
pandas apply多线程实现代码
Aug 17 Python
python 解决微分方程的操作(数值解法)
May 26 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实现水仙花数的4个示例分享
2014/04/08 PHP
PHP自定义多进制的方法
2016/11/03 PHP
PHP编程获取音频文件时长的方法【基于getid3类】
2017/04/20 PHP
PHP构造二叉树算法示例
2017/06/21 PHP
ThinkPHP5+Layui实现图片上传加预览功能
2018/08/17 PHP
php实现的PDO异常处理操作分析
2018/12/27 PHP
JavaScript 常见对象类创建代码与优缺点分析
2009/12/07 Javascript
JavaScript中为什么null==0为false而null大于=0为true(个人研究)
2013/09/16 Javascript
SeaJS入门教程系列之SeaJS介绍(一)
2014/03/03 Javascript
SyntaxHighlighter 3.0.83使用笔记
2015/01/26 Javascript
javascript常用方法总结
2015/05/14 Javascript
js实现横向伸展开的二级导航菜单代码
2015/08/28 Javascript
AngularJS中使用HTML5手机摄像头拍照
2016/02/22 Javascript
JavaScript:Date类型全面解析
2016/05/19 Javascript
实用又漂亮的BootstrapValidator表单验证插件
2016/05/30 Javascript
js 获取站点应用名的简单实例
2016/08/18 Javascript
setTimeout学习小结
2017/02/08 Javascript
js获取元素下的第一级子元素的方法(推荐)
2017/03/05 Javascript
微信小程序 自动登陆PHP源码实例(源码下载)
2017/05/08 Javascript
react router 4.0以上的路由应用详解
2017/09/21 Javascript
vue+axios+promise实际开发用法详解
2018/10/15 Javascript
小程序开发之模态框组件封装
2020/04/23 Javascript
vue+springboot+element+vue-resource实现文件上传教程
2020/10/21 Javascript
Javascript 模拟mvc实现点餐程序案例详解
2020/12/24 Javascript
Vue通过阿里云oss的url连接直接下载文件并修改文件名的方法
2020/12/25 Vue.js
详解Python编程中基本的数学计算使用
2016/02/04 Python
Python实现购物系统(示例讲解)
2017/09/13 Python
在Python中使用MySQL--PyMySQL的基本使用方法
2019/11/19 Python
Python自动化测试中yaml文件读取操作
2020/08/20 Python
PyTorch中的拷贝与就地操作详解
2020/12/09 Python
Nili Lotan官网:Nili Lotan同名品牌
2018/01/07 全球购物
JSF界面控制层技术
2013/06/17 面试题
小学教师国培感言
2014/02/08 职场文书
补充协议书
2015/01/28 职场文书
2015年农村党员干部主题教育活动总结
2015/03/25 职场文书
vue3引入highlight.js进行代码高亮的方法实例
2022/04/08 Vue.js