常见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中使用Mako模版库的简单教程
Apr 08 Python
Django的信号机制详解
May 05 Python
Python常见异常分类与处理方法
Jun 04 Python
深入理解python中函数传递参数是值传递还是引用传递
Nov 07 Python
Python 3.6 读取并操作文件内容的实例
Apr 23 Python
python+selenium实现自动抢票功能实例代码
Nov 23 Python
Python sklearn KFold 生成交叉验证数据集的方法
Dec 11 Python
python代码 输入数字使其反向输出的方法
Dec 22 Python
Python在图片中插入大量文字并且自动换行
Jan 02 Python
关于Python解包知识点总结
May 05 Python
Python函数参数分类原理详解
May 28 Python
python压包的概念及实例详解
Feb 17 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 静态变量的初始化
2009/11/15 PHP
php二分查找二种实现示例
2014/03/12 PHP
jQuery学习笔记 操作jQuery对象 属性处理
2012/09/19 Javascript
js 得到文件后缀(通过正则实现)
2013/07/08 Javascript
页面载入结束自动调用js函数示例
2013/09/23 Javascript
JavaScript?Apple设备检测示例代码
2013/11/15 Javascript
javascript 构造函数方式定义对象
2015/01/02 Javascript
Java Mybatis框架入门基础教程
2015/09/21 Javascript
多个js毫秒倒计时同时进行效果
2016/01/05 Javascript
JavaScript版经典游戏之扫雷游戏完整示例【附demo源码下载】
2016/12/12 Javascript
详解Node.js实现301、302重定向服务
2017/04/07 Javascript
详解从angular-cli:1.0.0-beta.28.3升级到@angular/cli:1.0.0
2017/05/22 Javascript
AngularJS 教程及实例代码
2017/10/23 Javascript
jQuery.Sumoselect插件实现下拉复选框效果
2017/11/09 jQuery
微信小程序左右滑动的实现代码
2017/12/15 Javascript
如何开发出更好的JavaScript模块
2017/12/22 Javascript
不使用JavaScript实现菜单的打开和关闭效果demo
2018/05/01 Javascript
微信小程序实现点击图片旋转180度并且弹出下拉列表
2018/11/27 Javascript
highCharts提示框中显示当前时间的方法
2019/01/18 Javascript
Python深入学习之上下文管理器
2014/08/31 Python
跟老齐学Python之玩转字符串(2)更新篇
2014/09/28 Python
python Spyder界面无法打开的解决方法
2018/04/27 Python
Sanic框架异常处理与中间件操作实例分析
2018/07/16 Python
Python提取频域特征知识点浅析
2019/03/04 Python
python实现随机漫步方法和原理
2019/06/10 Python
如何使用python爬虫爬取要登陆的网站
2019/07/12 Python
python中时间转换datetime和pd.to_datetime详析
2019/08/11 Python
pycharm显示远程图片的实现
2019/11/04 Python
Tensorflow中k.gradients()和tf.stop_gradient()用法说明
2020/06/10 Python
详解通过focusout事件解决IOS键盘收起时界面不归位的问题
2019/07/18 HTML / CSS
Staples美国官方网站:办公用品一站式采购
2016/07/28 全球购物
行政人员岗位职责
2013/12/08 职场文书
环保标语大全
2014/06/12 职场文书
节约粮食标语
2014/06/18 职场文书
驳回起诉裁定书
2015/05/19 职场文书
Ruby处理YAML和json数据
2022/04/18 Ruby