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 26 Python
Python实现读取字符串按列分配后按行输出示例
Apr 17 Python
查看TensorFlow checkpoint文件中的变量名和对应值方法
Jun 14 Python
flask框架使用orm连接数据库的方法示例
Jul 16 Python
对python中的乘法dot和对应分量相乘multiply详解
Nov 14 Python
Python使用线程来接收串口数据的示例
Jul 02 Python
Python流程控制 if else实现解析
Sep 02 Python
Python requests及aiohttp速度对比代码实例
Jul 16 Python
利用Python pandas对Excel进行合并的方法示例
Nov 04 Python
python 批量下载bilibili视频的gui程序
Nov 20 Python
Django显示可视化图表的实践
May 10 Python
python 中的jieba分词库
Nov 23 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
IIS php环境配置PHP5 MySQL5 ZendOptimizer phpmyadmin安装与配置
2008/11/18 PHP
解析mysql 表中的碎片产生原因以及清理
2013/06/22 PHP
php实现将字符串按照指定距离进行分割的方法
2015/03/14 PHP
在php的yii2框架中整合hbase库的方法
2018/09/20 PHP
prototype1.4中文手册
2006/09/22 Javascript
事件模型在各浏览器中存在差异
2010/10/20 Javascript
一个Action如何调用两个不同的方法
2014/05/22 Javascript
js实现二代身份证号码验证详解
2014/11/20 Javascript
理解JavaScript中Promise的使用
2016/01/18 Javascript
使用jQuery Mobile框架开发移动端Web App的入门教程
2016/05/17 Javascript
浅谈js键盘事件全面控制
2016/12/01 Javascript
jQuery实现的仿百度,仿谷歌搜索下拉框效果示例
2016/12/30 Javascript
JavaScript实现计数器基础方法
2017/10/10 Javascript
前端Electron新手入门教程详解
2019/06/21 Javascript
Vue-cli3.x + axios 跨域方案踩坑指北
2019/07/04 Javascript
[15:46]教你分分钟做大人——沙王
2015/03/11 DOTA
Python编程中装饰器的使用示例解析
2016/06/20 Python
python魔法方法-自定义序列详解
2016/07/21 Python
Python编程使用NLTK进行自然语言处理详解
2017/11/16 Python
Selenium定位元素操作示例
2018/08/10 Python
利用python将图片版PDF转文字版PDF
2019/05/03 Python
python中的TCP(传输控制协议)用法实例分析
2019/11/15 Python
python中的RSA加密与解密实例解析
2019/11/18 Python
python 导入数据及作图的实现
2019/12/03 Python
pytorch三层全连接层实现手写字母识别方式
2020/01/14 Python
keras:model.compile损失函数的用法
2020/07/01 Python
Python requests及aiohttp速度对比代码实例
2020/07/16 Python
利用css3 translate完美实现表头固定效果
2017/02/28 HTML / CSS
CSS3属性选择符介绍
2008/10/17 HTML / CSS
CSS3教程(1):什么是CSS3
2009/04/02 HTML / CSS
什么是类的返射机制
2016/02/06 面试题
简历中自我评价范文3则
2013/12/14 职场文书
2014年公司工作总结
2014/11/22 职场文书
2014年机关工会工作总结
2014/12/19 职场文书
python实现简易名片管理系统
2021/04/11 Python
Python中seaborn库之countplot的数据可视化使用
2021/06/11 Python