常见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 相关文章推荐
Python编写检测数据库SA用户的方法
Jul 11 Python
Python使用Selenium+BeautifulSoup爬取淘宝搜索页
Feb 24 Python
利用python实现微信头像加红色数字功能
Mar 26 Python
python实现列表中由数值查到索引的方法
Jun 27 Python
python装饰器原理与用法深入详解
Dec 19 Python
使用OpenCV circle函数图像上画圆的示例代码
Dec 27 Python
Python基于codecs模块实现文件读写案例解析
May 11 Python
Python参数传递机制传值和传引用原理详解
May 22 Python
OpenCV实现机器人对物体进行移动跟随的方法实例
Nov 09 Python
python 用pandas实现数据透视表功能
Dec 21 Python
matplotlib绘制鼠标的十字光标的实现(内置方式)
Jan 06 Python
Python标准库pathlib操作目录和文件
Nov 20 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
农民C键的运用技巧
2020/03/04 星际争霸
基于PHP+jQuery+MySql实现红蓝(顶踩)投票代码
2015/08/25 PHP
PHP实现的一致性哈希算法完整实例
2015/11/14 PHP
php从身份证获取性别和出生年月
2017/02/09 PHP
彻底搞懂JS无缝滚动代码
2007/01/03 Javascript
ie 处理 gif动画 的onload 事件的一个 bug
2007/04/12 Javascript
JavaScript 高级篇之闭包、模拟类,继承(五)
2012/04/07 Javascript
JavaScript 上万关键字瞬间匹配实现代码
2013/07/07 Javascript
jQuery实现简单隔行变色的方法
2016/02/20 Javascript
JS取模、取商及取整运算方法示例
2016/10/13 Javascript
jQuery插件扩展实例【添加回调函数】
2016/11/26 Javascript
JavaScript利用Date实现简单的倒计时实例
2017/01/12 Javascript
解决给dom元素绑定click等事件无效问题的方法
2017/02/17 Javascript
Vue render深入开发讲解
2018/04/13 Javascript
小程序图片剪裁加旋转的示例代码
2018/07/10 Javascript
bootstrap-table formatter 使用vue组件的方法
2019/05/09 Javascript
vue Tab切换以及缓存页面处理的几种方式
2019/11/05 Javascript
详解JS深拷贝与浅拷贝
2020/08/04 Javascript
[02:45]DOTA2英雄基础教程 伐木机
2013/12/23 DOTA
跟老齐学Python之玩转字符串(3)
2014/09/14 Python
Python通过正则表达式选取callback的方法
2015/07/18 Python
浅谈Python单向链表的实现
2015/12/24 Python
基于python yield机制的异步操作同步化编程模型
2016/03/18 Python
Python 常用 PEP8 编码规范详解
2017/01/22 Python
Python OpenCV获取视频的方法
2018/02/28 Python
关于Python的一些学习总结
2018/05/25 Python
Python脚本如何在bilibili中查找弹幕发送者
2020/06/04 Python
Python常用数据分析模块原理解析
2020/07/20 Python
Pycharm自带Git实现版本管理的方法步骤
2020/09/18 Python
G-Form护具官方网站:美国运动保护装备
2019/09/04 全球购物
英国时尚首饰品牌:Missoma
2020/06/29 全球购物
个人社会实践自我鉴定
2014/03/24 职场文书
小学生植树节活动总结
2014/07/04 职场文书
法定代表人身份证明书(含说明)
2014/10/02 职场文书
校长新学期致辞
2015/07/30 职场文书
浅谈MySQL函数
2021/10/05 MySQL