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 相关文章推荐
解决Django模板无法使用perms变量问题的方法
Sep 10 Python
tensorflow实现softma识别MNIST
Mar 12 Python
Python实现的直接插入排序算法示例
Apr 29 Python
利用Anaconda简单安装scrapy框架的方法
Jun 13 Python
python 将列表中的字符串连接成一个长路径的方法
Oct 23 Python
Python学习笔记之图片人脸检测识别实例教程
Mar 06 Python
关于jupyter打开之后不能直接跳转到浏览器的解决方式
Apr 13 Python
python模拟哔哩哔哩滑块登入验证的实现
Apr 24 Python
基于python实现查询ip地址来源
Jun 02 Python
Python基于数列实现购物车程序过程详解
Jun 09 Python
Python列表元素删除和remove()方法详解
Jan 04 Python
利用Python多线程实现图片下载器
Mar 25 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
广播爱好者需要了解的天线知识
2021/03/01 无线电
php使用Session和文件统计在线人数
2015/07/04 PHP
PHP封装的验证码工具类定义与用法示例
2018/08/22 PHP
总结一些js自定义的函数
2006/08/05 Javascript
jquery.alert 弹出式复选框实现代码
2009/06/15 Javascript
加速IE的Javascript document输出的方法
2010/12/02 Javascript
js 判断checkbox是否选中的操作方法
2012/11/09 Javascript
jQuery对象和Javascript对象之间转换的实例代码
2013/03/20 Javascript
jquery中prop()方法和attr()方法的区别浅析
2013/09/06 Javascript
JS加jquery简单实现标签元素的显示或隐藏
2013/09/23 Javascript
JS保留两位小数,多位小数的示例代码
2014/01/07 Javascript
使用js画图之正弦曲线
2015/01/12 Javascript
JavaScript实现的MD5算法完整实例
2016/02/02 Javascript
微信小程序 在Chrome浏览器上运行以及WebStorm的使用
2016/09/27 Javascript
JS中用EL表达式获取上下文参数值的方法
2018/03/28 Javascript
用npm安装vue和vue-cli,并使用webpack创建项目的方法
2018/09/28 Javascript
微信小程序实现录制、试听、上传音频功能(带波形图)
2020/02/27 Javascript
使用vue实现通过变量动态拼接url
2020/07/22 Javascript
小程序自定义弹框效果
2020/11/16 Javascript
采用Psyco实现python执行速度提高到与编译语言一样的水平
2014/10/11 Python
Python合并多个装饰器小技巧
2015/04/28 Python
在NumPy中创建空数组/矩阵的方法
2018/06/15 Python
Python中循环后使用list.append()数据被覆盖问题的解决
2018/07/01 Python
python实现事件驱动
2018/11/21 Python
用python求一个数组的和与平均值的实现方法
2019/06/29 Python
Python文本处理简单易懂方法解析
2019/12/19 Python
美国百货齐全的精品网站,提供美式风格的产品:Overstock.com
2016/07/22 全球购物
SmartBuyGlasses丹麦:网上购买名牌太阳镜、眼镜和隐形眼镜
2016/10/01 全球购物
Puccini乌克兰:购买行李箱、女士手袋网上商店
2020/08/06 全球购物
旅游安全协议书
2014/04/21 职场文书
2014年检验员工作总结
2014/11/19 职场文书
2015年管理人员工作总结
2015/05/13 职场文书
搭讪开场白台词大全
2015/05/28 职场文书
男方家长婚礼答谢词
2015/09/29 职场文书
什么是执行力?9个故事告诉您:成功绝非偶然!
2019/07/05 职场文书
处世之道:关于真诚相待的名言推荐
2019/12/02 职场文书