python的re模块使用方法详解


Posted in Python onJuly 26, 2019

一、正则表达式的特殊字符介绍

正则表达式
^      匹配行首                  
$      匹配行尾                  
.      任意单个字符          
[]     匹配包含在中括号中的任意字符
[^]     匹配包含在中括号中的字符之外的字符
[-]     匹配指定范围的任意单个字符
?     匹配之前项的1次或者0次
+      匹配之前项的1次或者多次
*      匹配之前项的0次或者多次
{n}     匹配之前项的n次
{m,n}    匹配之前项最大n次,最小m次
{n,}    配置之前项至少n次

二、re模块的方法介绍

1、匹配类方法

a、findall方法

# findall方法,该方法在字符串中查找模式匹配,将所有的匹配字符串以列表的形式返回,如果文本中没有任何字符串匹配模式,则返回一个空的列表,
# 如果有一个子字符串匹配模式,则返回包含一个元素的列表,所以,无论怎么匹配,我们都可以直接遍历findall返回的结果而不会出错,这对工程师
# 编写程序来说,减少了异常情况的处理,代码逻辑更加简洁
 
# re.findall() 用来输出所有符合模式匹配的子串
 
re_str = "hello this is python 2.7.13 and python 3.4.5"
 
pattern = "python [0-9]\.[0-9]\.[0-9]"
res = re.findall(pattern=pattern,string=re_str)
print(res)
 
# ['python 2.7.1', 'python 3.4.5']
 
pattern = "python [0-9]\.[0-9]\.[0-9]{2,}"
res = re.findall(pattern=pattern,string=re_str)
print(res)
 
# ['python 2.7.13']
 
 
pattern = "python[0-9]\.[0-9]\.[0-9]{2,}"
res = re.findall(pattern=pattern,string=re_str)
print(res)
 
# []
 
# re.findall() 方法,返回一个列表,如果匹配到的话,列表中的元素为匹配到的子字符串,如果没有匹配到,则返回一个空的列表
 
re_str = "hello this is python 2.7.13 and Python 3.4.5"
 
pattern = "python [0-9]\.[0-9]\.[0-9]"
res = re.findall(pattern=pattern,string=re_str,flags=re.IGNORECASE)
print(res)
 
# ['python 2.7.1', 'Python 3.4.5']
 
# 设置标志flags=re.IGNORECASE,意思为忽略大小写

b、编译的方式使用正则表达式

# 我们一般采用编译的方式使用python的正则模块,如果在大量的数据量中,编译的方式使用正则性能会提高很多,具体读者们可以可以实际测试
re_str = "hello this is python 2.7.13 and Python 3.4.5"
re_obj = re.compile(pattern = "python [0-9]\.[0-9]\.[0-9]",flags=re.IGNORECASE)
res = re_obj.findall(re_str)
print(res)

c、match方法

# match方法,类似于字符串中的startwith方法,只是match应用在正则表达式中更加强大,更富有表现力,match函数用以匹配字符串的开始部分,如果模式
# 匹配成功,返回一个SRE_Match类型的对象,如果模式匹配失败,则返回一个None,因此对于普通的前缀匹配,他的用法几乎和startwith一模一样,例如我
# 们要判断data字符串是否以what和是否以数字开头
s_true = "what is a boy"
s_false = "What is a boy"
re_obj = re.compile("what")
 
print(re_obj.match(string=s_true))
# <_sre.SRE_Match object; span=(0, 4), match='what'
 
print(re_obj.match(string=s_false))
# None
 
s_true = "123what is a boy"
s_false = "what is a boy"
 
re_obj = re.compile("\d+")
 
print(re_obj.match(s_true))
# <_sre.SRE_Match object; span=(0, 3), match='123'>
 
print(re_obj.match(s_true).start())
# 0
print(re_obj.match(s_true).end())
# 3
print(re_obj.match(s_true).string)
# 123what is a boy
print(re_obj.match(s_true).group())
# 123
 
 
print(re_obj.match(s_false))
# None

d、search方法

# search方法,模式匹配成功后,也会返回一个SRE_Match对象,search方法和match的方法区别在于match只能从头开始匹配,而search可以从
# 字符串的任意位置开始匹配,他们的共同点是,如果匹配成功,返回一个SRE_Match对象,如果匹配失败,返回一个None,这里还要注意,
# search仅仅查找第一次匹配,也就是说一个字符串中包含多个模式的匹配,也只会返回第一个匹配的结果,如果要返回所有的结果,最简单
# 的方法就是findall方法,也可以使用finditer方法

e、finditer方法

# finditer返回一个迭代器,遍历迭代器可以得到一个SRE_Match对象,比如下面的例子
re_str = "what is a different between python 2.7.14 and python 3.5.4"
 
re_obj = re.compile("\d{1,}\.\d{1,}\.\d{1,}")
 
for i in re_obj.finditer(re_str):
  print(i)
 
# <_sre.SRE_Match object; span=(35, 41), match='2.7.14'>
# <_sre.SRE_Match object; span=(53, 58), match='3.5.4'>

2、修改类方法介绍

a、sub方法

# re模块sub方法类似于字符串中的replace方法,只是sub方法支持使用正则表达式,所以,re模块的sub方法使用场景更加广泛
re_str = "what is a different between python 2.7.14 and python 3.5.4"
 
re_obj = re.compile("\d{1,}\.\d{1,}\.\d{1,}")
 
print(re_obj.sub("a.b.c",re_str,count=1))
# what is a different between python a.b.c and python 3.5.4
 
print(re_obj.sub("a.b.c",re_str,count=2))
# what is a different between python a.b.c and python a.b.c
 
print(re_obj.sub("a.b.c",re_str))
# what is a different between python a.b.c and python a.b.c

b、split方法

# re模块的split方法和python字符串中的split方法功能是一样的,都是将一个字符串拆分成子字符串的列表,区别在于re模块的split方法能够
# 使用正则表达式
# 比如下面的例子,使用. 空格 : !分割字符串,返回的是一个列表
re_str = "what is a different between python 2.7.14 and python 3.5.4 USA:NewYork!Zidan.FRA"
 
re_obj = re.compile("[. :!]")
 
print(re_obj.split(re_str))
# ['what', 'is', 'a', 'different', 'between', 'python', '2', '7', '14', 'and', 'python', '3', '5', '4', 'USA', 'NewYork', 'Zidan', 'FRA']

c、大小写不敏感设置

# 3、大小写不敏感
 
# re.compile(flags=re.IGNORECASE)

d、非贪婪匹配

# 4、非贪婪匹配,贪婪匹配总是匹配到最长的那个字符串,相应的,非贪婪匹配是匹配到最小的那个字符串,只需要在匹配字符串的时候加一个?即可
 
# 下面的例子,注意两个.
s = "Beautiful is better than ugly.Explicit is better than impliciy."
 
 
re_obj = re.compile("Beautiful.*y\.")
 
print(re_obj.findall(s))
# ['Beautiful is better than ugly.Explicit is better than implicit.']
 
re_obj = re.compile("Beautiful.*?\.")
 
print(re_obj.findall(s))
# ['Beautiful is better than ugly.']

e、在正则匹配字符串中加一个小括号,会有什么的效果呢?

如果是要配置一个真正的小括号,那么就需要转义符,下面的例子大家仔细看下,注意下search方法返回的对象的group(1)这个方法是报错的

import re
s = "=aa1239d&&& 0a ()--"
 
# obj = re.compile("\(\)")
# search
# rep = obj.search(s)
# print(rep)
# <_sre.SRE_Match object; span=(15, 17), match='()'>
# print(rep.group(1))
# IndexError: no such group
# print(rep.group())
# ()
# findall
 
rep = obj.findall(s)
print(rep)
# ['()']

如果是要返回括号中匹配的字符串中,则该小括号不需要转义符,findall方法返回的是小伙好中匹配到的字符串,search.group()方法的返回的整个模式匹配到字符串,search.group(1)这个是匹配第一个小括号中的模式匹配到的字符串,search.group(2)这个是匹配第二个小括号中的模式匹配到的字符串,以此类推

s = "=aa1239d&&& 0a ()--"
rep = re.compile("\w+(&+)")
 
print(rep.findall(s))
# ['&&&']
print(rep.search(s).group())
# aa1239d&&&
print(rep.search(s).group(1))
# &&&

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python代码的打包与发布详解
Jul 30 Python
Python自定义scrapy中间模块避免重复采集的方法
Apr 07 Python
TF-IDF与余弦相似性的应用(二) 找出相似文章
Dec 21 Python
Python实现简单http服务器
Apr 12 Python
解决csv.writer写入文件有多余的空行问题
Jul 06 Python
python的faker库用法
Nov 28 Python
使用pandas 将DataFrame转化成dict
Dec 10 Python
python django中8000端口被占用的解决
Dec 17 Python
Python sklearn库实现PCA教程(以鸢尾花分类为例)
Feb 24 Python
Django User 模块之 AbstractUser 扩展详解
Mar 11 Python
selenium自动化测试入门实战
Dec 21 Python
Python使用BeautifulSoup4修改网页内容
May 20 Python
Python企业编码生成系统总体系统设计概述
Jul 26 #Python
详解程序意外中断自动重启shell脚本(以Python为例)
Jul 26 #Python
python的pstuil模块使用方法总结
Jul 26 #Python
python爬虫项目设置一个中断重连的程序的实现
Jul 26 #Python
python通过http下载文件的方法详解
Jul 26 #Python
快速解决vue.js 模板和jinja 模板冲突的问题
Jul 26 #Python
Python调用C语言的实现
Jul 26 #Python
You might like
php 随机数的产生、页面跳转、件读写、文件重命名、switch语句
2009/08/07 PHP
php 求质素(素数) 的实现代码
2011/04/12 PHP
ThinkPHP整合百度Ueditor图文教程
2014/10/21 PHP
PHP使用逆波兰式计算工资的方法
2015/07/29 PHP
php实现图片缩略图的方法
2016/03/29 PHP
MAC下通过改apache配置文件切换php多版本的方法
2017/04/26 PHP
一些不错的js函数ajax
2008/08/20 Javascript
JavaScript 滚轮事件使用说明
2010/03/07 Javascript
Dom在ajax技术中的作用说明
2010/10/25 Javascript
JQuery文本改变触发事件如聚焦事件、失焦事件
2014/01/15 Javascript
jquery实现的网页自动播放声音
2014/04/30 Javascript
Jquery注册事件实现方法
2015/05/18 Javascript
原生js实现放大镜效果
2017/01/11 Javascript
Angular2 组件通信的实例代码
2017/06/23 Javascript
vue数据传递--我有特殊的实现技巧
2018/03/20 Javascript
vue引入js数字小键盘的实现代码
2018/05/14 Javascript
vue回到顶部监听滚动事件详解
2019/08/02 Javascript
[04:54]DOTA2-DPC中国联赛1月31日Recap集锦
2021/03/11 DOTA
用python实现简单EXCEL数据统计的实例
2017/01/24 Python
python中的内置函数max()和min()及mas()函数的高级用法
2018/03/29 Python
python实现简单登陆流程的方法
2018/04/22 Python
Python定时发送消息的脚本:每天跟你女朋友说晚安
2018/10/21 Python
python基于K-means聚类算法的图像分割
2019/10/30 Python
Django项目基础配置和基本使用过程解析
2019/11/25 Python
实例讲解CSS3中的box-flex弹性盒属性布局
2016/06/09 HTML / CSS
详解CSS3 rem(设置字体大小) 教程
2017/11/21 HTML / CSS
针对HTML5的Web Worker使用攻略
2015/07/12 HTML / CSS
八项规定整改措施
2014/02/12 职场文书
听课评语大全
2014/04/30 职场文书
大二学生学年自我鉴定
2014/09/12 职场文书
领导班子四风查摆对照检查材料思想汇报
2014/10/05 职场文书
2015年简历自我评价范文
2015/03/11 职场文书
幼儿园中秋节活动总结
2015/03/23 职场文书
儿子满月酒致辞
2015/07/29 职场文书
养成教育主题班会
2015/08/13 职场文书
python关于集合的知识案例详解
2021/05/30 Python