Python正则表达式使用经典实例


Posted in Python onJune 21, 2016

下面列出Python正则表达式的几种匹配用法,具体内容如下所示:

此外,关于正则的一切http://deerchao.net/tutorials/regex/regex.htm

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的绘图工具matplotlib使用实例
Jul 03 Python
实例讲解Python中的私有属性
Aug 21 Python
用Python实现一个简单的多线程TCP服务器的教程
May 05 Python
python实现搜索指定目录下文件及文件内搜索指定关键词的方法
Jun 28 Python
编写自定义的Django模板加载器的简单示例
Jul 21 Python
Python中datetime模块参考手册
Jan 13 Python
python 列表降维的实例讲解
Jun 28 Python
python 输出所有大小写字母的方法
Jan 02 Python
Python 3.8中实现functools.cached_property功能
May 29 Python
python命令行工具Click快速掌握
Jul 04 Python
Python使用Excel将数据写入多个sheet
May 16 Python
教你使用Pandas直接核算Excel中快递费用
May 12 Python
常见的python正则用法实例讲解
Jun 21 #Python
实例讲解Python的函数闭包使用中应注意的问题
Jun 20 #Python
Python中的数学运算操作符使用进阶
Jun 20 #Python
Python中在for循环中嵌套使用if和else语句的技巧
Jun 20 #Python
解析Python中的生成器及其与迭代器的差异
Jun 20 #Python
Python判断列表是否已排序的各种方法及其性能分析
Jun 20 #Python
Python编程中装饰器的使用示例解析
Jun 20 #Python
You might like
简单采集了yahoo的一些数据
2007/02/14 PHP
相对路径转化成绝对路径
2007/04/10 PHP
php+mysql 实现身份验证代码
2010/03/24 PHP
解析php入库和出库
2013/06/25 PHP
Yii学习总结之安装配置
2015/02/22 PHP
php单一接口的实现方法
2015/06/20 PHP
PHP实现批量上传单个文件
2015/12/29 PHP
深入剖析浏览器退出之后php还会继续执行么
2016/05/17 PHP
php函数传值的引用传递注意事项分析
2016/06/25 PHP
thinkphp3.2嵌入百度编辑器ueditor的实例代码
2017/07/13 PHP
ExtJS GridPanel 根据条件改变字体颜色
2010/03/08 Javascript
SlideView 图片滑动(扩展/收缩)展示效果
2010/08/01 Javascript
基于jquery实现的可编辑下拉框实现代码
2014/08/02 Javascript
常用的Javascript数据验证插件
2015/08/04 Javascript
解决jQuery使用JSONP时产生的错误
2015/12/02 Javascript
vue.js通过自定义指令实现数据拉取更新的实现方法
2016/10/18 Javascript
解析利用javascript如何判断一个数为素数
2016/12/08 Javascript
JS正则表达式验证密码格式的集中情况总结
2017/02/23 Javascript
详解vue-cli项目中的proxyTable跨域问题小结
2018/02/09 Javascript
快速解决vue在ios端下点击响应延时的问题
2018/08/27 Javascript
浅析Vue.js 中的条件渲染指令
2018/11/19 Javascript
[05:15]DOTA2英雄梦之声_第16期_灰烬之灵
2014/06/21 DOTA
使用python编写脚本获取手机当前应用apk的信息
2014/07/21 Python
Python中的localtime()方法使用详解
2015/05/22 Python
python执行使用shell命令方法分享
2017/11/08 Python
Python3模拟登录操作实例分析
2019/03/12 Python
Python使用matplotlib绘制三维参数曲线操作示例
2019/09/10 Python
Django扫码抽奖平台的配置过程详解
2021/01/14 Python
python 实现图片裁剪小工具
2021/02/02 Python
html5 利用canvas手写签名并保存的实现方法
2018/07/12 HTML / CSS
办公室内勤工作职责
2013/12/11 职场文书
房屋出租协议书
2014/04/10 职场文书
大学生职业生涯规划大赛作品(精品)
2014/09/17 职场文书
作风整顿个人剖析材料
2014/10/06 职场文书
2014年妇女工作总结
2014/12/06 职场文书
职代会闭幕词
2015/01/28 职场文书