常见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的词法分析与语法分析
May 18 Python
详解Python中的from..import绝对导入语句
Jun 21 Python
Python用UUID库生成唯一ID的方法示例
Dec 15 Python
python3中str(字符串)的使用教程
Mar 23 Python
python 3.6 tkinter+urllib+json实现火车车次信息查询功能
Dec 20 Python
python决策树之C4.5算法详解
Dec 20 Python
python 整数越界问题详解
Jun 27 Python
python实现微信自动回复机器人功能
Jul 11 Python
python opencv将图片转为灰度图的方法示例
Jul 31 Python
解决python3 requests headers参数不能有中文的问题
Aug 21 Python
python 实现从高分辨图像上抠取图像块
Jan 02 Python
python Tensor和Array对比分析
Jan 08 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
UCenter Home二次开发指南
2009/05/28 PHP
PHP句法规则详解 入门学习
2011/11/09 PHP
深入php多态的实现详解
2013/06/09 PHP
php中time()与$_SERVER[REQUEST_TIME]用法区别
2014/11/19 PHP
php魔术函数__call()用法实例分析
2015/02/13 PHP
PHP中创建和验证哈希的简单方法实探
2015/07/06 PHP
php获取是星期几的的一些常用姿势
2019/12/15 PHP
再论Javascript的类继承
2011/03/05 Javascript
更换select下拉菜单背景样式的实现代码
2011/12/20 Javascript
在js(jquery)中获得文本框焦点和失去焦点的方法
2012/12/04 Javascript
Javascript 检测键盘按键信息及键码值对应介绍
2013/01/03 Javascript
Jquery中children与find之间的区别详细解析
2013/11/29 Javascript
javascript获取当前的时间戳的方法汇总
2015/07/26 Javascript
微信小程序 Flex布局详解
2016/10/09 Javascript
js正则表达式惰性匹配和贪婪匹配用法分析
2016/12/26 Javascript
微信小程序 两种滑动方式(横向滑动,竖向滑动)详细及实例代码
2017/01/13 Javascript
JS监控关闭浏览器操作的实例详解
2017/09/12 Javascript
jQuery的ztree仿windows文件新建和拖拽功能的实现代码
2018/12/05 jQuery
深入解析koa之异步回调处理
2019/06/17 Javascript
vue 遮罩层阻止默认滚动事件操作
2020/07/28 Javascript
vue keep-alive的简单总结
2021/01/25 Vue.js
Python 50行爬虫抓取并处理图灵书目过程详解
2019/09/20 Python
Jmeter调用Python脚本实现参数互相传递的实现
2021/01/22 Python
viagogo波兰票务平台:演唱会、体育比赛、戏剧门票
2018/04/23 全球购物
HolidayLettings英国:预订最好的度假公寓、别墅和自助式住宿
2019/08/27 全球购物
俄罗斯连接商品和买家的在线平台:goods.ru
2020/11/30 全球购物
金蝶的一道SQL笔试题
2012/12/18 面试题
Python里面如何实现tuple和list的转换
2012/06/13 面试题
营销主管自我评价怎么写
2013/09/19 职场文书
财政专业求职信范文
2014/02/19 职场文书
师范教师专业大学生职业生涯规划范文
2014/03/02 职场文书
俞敏洪励志演讲稿
2014/04/29 职场文书
植树节新闻稿
2015/07/17 职场文书
php双向队列实例讲解
2021/11/17 PHP
SpringBoot整合minio快速入门教程(代码示例)
2022/04/03 Java/Android
vue数据字典取键值项目的字典问题
2022/04/12 Vue.js