常见python正则用法的简单实例


Posted in Python onJune 21, 2016

下面列出Python正则表达式的几种匹配用法:

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 相关文章推荐
详谈Python2.6和Python3.0中对除法操作的异同
Apr 28 Python
python对excel文档去重及求和的实例
Apr 18 Python
python matplotlib绘图,修改坐标轴刻度为文字的实例
May 25 Python
python和opencv实现抠图
Jul 18 Python
Linux下python制作名片示例
Jul 20 Python
[原创]Python入门教程1. 基本运算【四则运算、变量、math模块等】
Oct 28 Python
python字符串中匹配数字的正则表达式
Jul 03 Python
Python 、Pycharm、Anaconda三者的区别与联系、安装过程及注意事项
Oct 11 Python
Python MOCK SERVER moco模拟接口测试过程解析
Apr 13 Python
使用keras实现非线性回归(两种加激活函数的方式)
Jul 05 Python
python 实现简单的计算器(gui界面)
Nov 11 Python
Python爬虫入门教程02之笔趣阁小说爬取
Jan 24 Python
小议Python中自定义函数的可变参数的使用及注意点
Jun 21 #Python
简单讲解Python编程中namedtuple类的用法
Jun 21 #Python
Python编程中实现迭代器的一些技巧小结
Jun 21 #Python
Centos Python2 升级到Python3的简单实现
Jun 21 #Python
Python的Django框架中forms表单类的使用方法详解
Jun 21 #Python
Python正则表达式使用经典实例
Jun 21 #Python
常见的python正则用法实例讲解
Jun 21 #Python
You might like
解析php中mysql_connect与mysql_pconncet的区别详解
2013/05/15 PHP
php获取淘宝分类id示例
2014/01/16 PHP
php强大的时间转换函数strtotime
2016/02/18 PHP
php 从指定数字中获取随机组合的简单方法(推荐)
2017/04/05 PHP
PHPMAILER实现PHP发邮件功能
2018/04/18 PHP
比较简单的一个符合web标准的JS调用flash方法
2007/11/29 Javascript
javascript import css实例代码
2008/07/18 Javascript
Javascript 鼠标移动上去小三角形滑块缓慢跟随效果
2013/04/26 Javascript
javascript中数组的冒泡排序使用示例
2013/12/18 Javascript
nodejs获取本机内网和外网ip地址的实现代码
2014/06/01 NodeJs
js跨域问题浅析及解决方法优缺点对比
2014/11/08 Javascript
浅谈javascript原型链与继承
2015/07/13 Javascript
smartcrop.js智能图片裁剪库
2015/10/14 Javascript
react-router实现按需加载
2017/05/09 Javascript
解决vue动态为数据添加新属性遇到的问题
2018/09/18 Javascript
浅谈element中InfiniteScroll按需引入的一点注意事项
2020/06/05 Javascript
design vue 表格开启列排序的操作
2020/10/28 Javascript
深入解析Python的Tornado框架中内置的模板引擎
2016/07/11 Python
python实现聚类算法原理
2018/02/12 Python
Python学习笔记之open()函数打开文件路径报错问题
2018/04/28 Python
Python + OpenCV 实现LBP特征提取的示例代码
2019/07/11 Python
Django 实现admin后台显示图片缩略图的例子
2019/07/28 Python
Python搭建代理IP池实现获取IP的方法
2019/10/27 Python
Pytorch DataLoader 变长数据处理方式
2020/01/08 Python
python实现IOU计算案例
2020/04/12 Python
pandas数据拼接的实现示例
2020/04/16 Python
解决keras,val_categorical_accuracy:,0.0000e+00问题
2020/07/02 Python
基于Python爬取京东双十一商品价格曲线
2020/10/23 Python
python使用scapy模块实现ping扫描的过程详解
2021/01/21 Python
Boston Proper官网:美国女装品牌
2017/10/30 全球购物
英国老牌潮鞋店:Offspring
2019/08/19 全球购物
2013年保送生自荐信格式
2013/11/20 职场文书
毕业学生推荐信
2013/12/01 职场文书
个人作风建设总结
2014/10/23 职场文书
收银员岗位职责
2015/02/03 职场文书
Python数据可视化之用Matplotlib绘制常用图形
2021/06/03 Python