Python中正则表达式的用法实例汇总


Posted in Python onAugust 18, 2014

正则表达式是Python程序设计中非常实用的功能,本文就常用的正则表达式做一汇总,供大家参考之用。具体如下:

一、字符串替换

1.替换所有匹配的子串

用newstring替换subject中所有与正则表达式regex匹配的子串

result, number = re.subn(regex, newstring, subject)

2.替换所有匹配的子串(使用正则表达式对象)

reobj = re.compile(regex)
result, number = reobj.subn(newstring, subject)

二、字符串拆分

1.字符串拆分

result = re.split(regex, subject)

2.字符串拆分(使用正则表示式对象)

reobj = re.compile(regex)
result = reobj.split(subject)

三、匹配

下面列出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. 创建一个匹配对象,然后通过该对象获得匹配细节

regex=ur"..." #正则表达式
match = re.search(regex, subject)
if match:
  # match start: match.start()
  # match end (exclusive): match.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): match.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): match.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()

感兴趣的读者可以动手调试一下本文实例代码,相信会有新的收获。

Python 相关文章推荐
Python入门篇之条件、循环
Oct 17 Python
Python中使用logging模块打印log日志详解
Apr 05 Python
Python利用Nagios增加微信报警通知的功能
Feb 18 Python
Python实现字典按照value进行排序的方法分析
Dec 23 Python
Atom的python插件和常用插件说明
Jul 08 Python
Python函数定义及传参方式详解(4种)
Mar 18 Python
python地震数据可视化详解
Jun 18 Python
解决.ui文件生成的.py文件运行不出现界面的方法
Jun 19 Python
Python函数中参数是传递值还是引用详解
Jul 02 Python
flask 框架操作MySQL数据库简单示例
Feb 02 Python
解决pycharm同一目录下无法import其他文件
Feb 12 Python
基于SpringBoot构造器注入循环依赖及解决方式
Apr 26 Python
python中enumerate的用法实例解析
Aug 18 #Python
Python采用raw_input读取输入值的方法
Aug 18 #Python
Python中Collection的使用小技巧
Aug 18 #Python
Python实现3行代码解简单的一元一次方程
Aug 18 #Python
Python统计列表中的重复项出现的次数的方法
Aug 18 #Python
Python中无限元素列表的实现方法
Aug 18 #Python
Python中实现字符串类型与字典类型相互转换的方法
Aug 18 #Python
You might like
PHP中的类型约束介绍
2015/05/11 PHP
PHP给源代码加密的几种方法汇总(推荐)
2018/02/06 PHP
学习YUI.Ext 第七天--关于View&amp;JSONView
2007/03/10 Javascript
JavaScript 事件参考手册
2008/12/24 Javascript
读jQuery之一(对象的组成)
2011/06/11 Javascript
基于jquery的跟随屏幕滚动代码
2012/07/24 Javascript
解决jquery1.9不支持browser对象的问题
2013/11/13 Javascript
javascript实现类似于新浪微博搜索框弹出效果的方法
2015/07/27 Javascript
每天一篇javascript学习小结(String对象)
2015/11/18 Javascript
javascript中select下拉框的用法总结
2016/01/07 Javascript
基于jQuery实现带动画效果超炫酷的弹出对话框(附源码下载)
2016/02/22 Javascript
JavaScript中instanceof运算符的使用示例
2016/06/08 Javascript
JavaScript面试开发常用的知识点总结
2016/08/08 Javascript
BootStrap入门教程(二)之固定的内置样式
2016/09/19 Javascript
jQuery实现级联下拉框实战(5)
2017/02/08 Javascript
js 作用域和变量详解
2017/02/16 Javascript
js自定义弹框插件的封装
2020/08/24 Javascript
vue2实现搜索结果中的搜索关键字高亮的代码
2018/08/29 Javascript
layui实现下拉复选功能的例子(包括数据的回显与上传)
2019/09/24 Javascript
vue 使用插槽分发内容操作示例【单个插槽、具名插槽、作用域插槽】
2020/03/06 Javascript
python实现自动更换ip的方法
2015/05/05 Python
Python生成器的使用方法和示例代码
2019/03/04 Python
详解Python odoo中嵌入html简单的分页功能
2019/05/29 Python
Python中的list与tuple集合区别解析
2019/10/12 Python
Python实现图片裁剪的两种方式(Pillow和OpenCV)
2019/10/30 Python
Django {{ MEDIA_URL }}无法显示图片的解决方式
2020/04/07 Python
python获得命令行输入的参数的两种方式
2020/11/02 Python
Django前后端分离csrf token获取方式
2020/12/25 Python
YSL Beauty加拿大官方商城:圣罗兰美妆加拿大
2017/05/15 全球购物
保护水资源的标语
2014/06/17 职场文书
公司委托书怎么写
2014/08/02 职场文书
2014年药房工作总结
2014/11/22 职场文书
结婚老公保证书
2015/02/26 职场文书
单位推荐信范文
2015/03/27 职场文书
python字典的元素访问实例详解
2021/07/21 Python
教你使用Ubuntu搭建DNS服务器
2022/09/23 Servers