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学习数据结构实例代码
May 11 Python
Python中的字符串类型基本知识学习教程
Feb 04 Python
python3中zip()函数使用详解
Jun 29 Python
Flask模拟实现CSRF攻击的方法
Jul 24 Python
python删除字符串中指定字符的方法
Aug 13 Python
PyQt5+requests实现车票查询工具
Jan 21 Python
python+openCV利用摄像头实现人员活动检测
Jun 22 Python
python跳出双层for循环的解决方法
Jun 24 Python
Python Lambda函数使用总结详解
Dec 11 Python
python爬虫开发之使用Python爬虫库requests多线程抓取猫眼电影TOP100实例
Mar 10 Python
django使用JWT保存用户登录信息
Apr 22 Python
手把手教你配置JupyterLab 环境的实现
Feb 02 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
ThinkPHP令牌验证实例
2014/06/18 PHP
ThinkPHP单字母函数(快捷方法)使用总结
2014/07/23 PHP
PHP生成静态HTML页面最简单方法示例
2015/04/09 PHP
PHP7引入的&quot;??&quot;和&quot;?:&quot;的区别讲解
2019/04/08 PHP
改变javascript函数内部this指针指向的三种方法
2010/04/23 Javascript
jquery星级插件、支持页面中多次使用
2012/03/25 Javascript
JavaScript中__proto__与prototype的关系深入理解
2012/12/04 Javascript
jQuery弹出(alert)select选择的值
2013/04/21 Javascript
JS页面延迟执行一些方法(整理)
2013/11/11 Javascript
js将控件隐藏及display属性的使用介绍
2013/12/30 Javascript
node.js中的fs.readdirSync方法使用说明
2014/12/17 Javascript
JS实现跟随鼠标的链接文字提示框效果
2015/08/06 Javascript
详解javascript实现瀑布流列式布局
2016/01/29 Javascript
javascript设置文本框光标的方法实例小结
2016/11/04 Javascript
利用babel将es6语法转es5的简单示例
2017/12/01 Javascript
JS实现左边列表移到到右边列表功能
2018/03/28 Javascript
JavaScript设计模式之缓存代理模式原理与简单用法示例
2018/08/07 Javascript
详解Axios 如何取消已发送的请求
2018/10/20 Javascript
NUXT SSR初级入门笔记(小结)
2019/12/16 Javascript
nuxt配置通过指定IP和端口访问的实现
2020/01/08 Javascript
[36:13]Mineski vs iG 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
对于Python编程中一些重用与缩减的建议
2015/04/14 Python
python轻松查到删除自己的微信好友
2016/01/10 Python
Python正则表达式教程之一:基础篇
2017/03/02 Python
浅谈python中requests模块导入的问题
2018/05/18 Python
python获取指定日期范围内的每一天,每个月,每季度的方法
2019/08/08 Python
Python数据可视化实现正态分布(高斯分布)
2019/08/21 Python
python绘制玫瑰的实现代码
2020/03/02 Python
新建文件时Pycharm中自动设置头部模板信息的方法
2020/04/17 Python
python suds访问webservice服务实现
2020/06/26 Python
python使用numpy中的size()函数实例用法详解
2021/01/29 Python
10个顶级Python实用库推荐
2021/03/04 Python
Ancheer官方户外和运动商店:销售电动自行车
2019/08/07 全球购物
乡镇保密工作责任书
2014/07/28 职场文书
2015年秋季校长开学典礼致辞
2015/07/29 职场文书
高中生物教学反思
2016/02/20 职场文书