常见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实现电子词典
Apr 23 Python
简单介绍Python中的try和finally和with方法
May 05 Python
对pandas数据判断是否为NaN值的方法详解
Nov 06 Python
详解Python文件修改的两种方式
Aug 22 Python
python的命名规则知识点总结
Oct 04 Python
Spring Cloud Feign高级应用实例详解
Dec 10 Python
pytorch 求网络模型参数实例
Dec 30 Python
python3中关于excel追加写入格式被覆盖问题(实例代码)
Jan 10 Python
python安装后的目录在哪里
Jun 21 Python
详解Python 循环嵌套
Jul 09 Python
python利用os模块编写文件复制功能——copy()函数用法
Jul 13 Python
python可视化之颜色映射详解
Sep 15 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语法(1)
2006/10/09 PHP
Discuz!5的PHP代码高亮显示插件(黑暗中的舞者更新)
2007/01/29 PHP
phpstrom使用xdebug配置方法
2013/12/17 PHP
浅析PHP的静态成员函数效率更高的原因
2014/06/13 PHP
php通过Chianz.com获取IP地址与地区的方法
2015/01/14 PHP
几行代码轻松实现PHP文件打包下载zip
2017/03/01 PHP
Javascript 遍历对象中的子对象
2009/07/03 Javascript
javascript event 事件解析
2011/01/31 Javascript
JavaScript splice()方法详解
2020/09/22 Javascript
Javascript 按位与赋值运算符 (&amp;=)使用介绍
2014/02/04 Javascript
jquery xMarquee实现文字水平无缝滚动效果
2014/04/29 Javascript
JavaScript获取网页中第一个图片id的方法
2015/04/03 Javascript
微信小程序  简单实例(阅读器)的实例开发
2016/09/29 Javascript
BootStrap Validator对于隐藏域验证和程序赋值即时验证的问题浅析
2016/12/01 Javascript
JavaScript实现汉字转换为拼音的库文件示例
2016/12/22 Javascript
Angular2实现组件交互的方法分析
2017/12/19 Javascript
[01:04:06]DOTA2上海特级锦标赛A组资格赛#2 Secret VS EHOME第一局
2016/02/26 DOTA
Python CSV模块使用实例
2015/04/09 Python
Python的MongoDB模块PyMongo操作方法集锦
2016/01/05 Python
python使用zip将list转为json的方法
2018/12/31 Python
python、Matlab求定积分的实现
2019/11/20 Python
python和pywin32实现窗口查找、遍历和点击的示例代码
2020/04/01 Python
HTML5播放实现rtmp流直播
2020/06/16 HTML / CSS
Madda Fella官网:美国冒险家服装品牌
2020/01/16 全球购物
意大利网上药房:Farmacia 33
2020/01/27 全球购物
会计专业自荐信范文
2013/12/02 职场文书
暑期研修感言
2014/02/17 职场文书
高三毕业典礼主持词
2014/03/27 职场文书
小学新学期寄语
2014/04/02 职场文书
教师对学生的评语
2014/04/28 职场文书
激励口号大全
2014/06/17 职场文书
学校四风问题对照检查材料思想汇报
2014/09/26 职场文书
机关干部三严三实心得体会
2014/10/13 职场文书
2015个人简历自我评价语
2015/03/11 职场文书
商务英语求职信范文
2015/03/19 职场文书
2016大一新生军训心得体会
2016/01/11 职场文书