Python的正则表达式

news/2024/10/22 0:59:19/

    学完Python的基础语法还远远不够,还需要学习一些进阶用法,Python的正则表达式就是其一,什么是正则表达式呢,就我理解,就是用来检索一些需要的信息,比如说从一段文本中找出其中全部的数字或者字母,或者再加一些限定范围都是可以的,学习这个可以帮助我们从大量的文本信息中快速找到我们需要的信息。

python中re模块提供了正则表达式的功能

下面来介绍几个常用的方法

1、match

匹配字符串

re.match()必须从字符串开头匹配!

match方法尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。

演示代码

import re

a = re.match('test','testasdtest')  

print(a)                             #返回一个匹配对象

print(a.group())                     #返回test,获取不到则报错

print(a.span())           #返回匹配结果的位置,左闭右开区间

print(re.match('test','atestasdtest'))  #返回None

结果:

<re.Match object; span=(0, 4), match='test'>
test
(0, 4)
None

 我们发现这个方法只可以找到第一个,确找不到后面的,后面我们改进

匹配单字符

. 匹配任意一个字符

\d 匹配数字

\D 匹配非数字

\s 匹配特殊字符,如空白,空格,tab等

\S 匹配非空白

\w 匹配单词、字符,如大小写字母,数字,_ 下划线

\W 匹配非单词字符

[ ] 匹配[ ]中列举的字符

[^2345] 不匹配2345中的任意一个

[a-z3-5] 匹配a-z或者3-5中的字符

演示代码如下

python">a = re.match('..','testasdtest')  print(a.group())   #输出te                            b = re.match('ab.','testasdtest')  print(b) #返回none,因为表达式是以固定的ab开头然后跟上通配符. 所以必须要先匹配上ab才会往后进行匹配a = re.match('\d\d','23es12testasdtest')  print(a)                              b = re.match('\d\d\d','23es12testasdtest')  print(b)   #要求匹配三个数字,匹配不到返回nonec = re.match('\d','es12testasdtest')  print(c)   #起始位置没有匹配成功,一样返回nonea = re.match('\D','23es12testasdtest')  print(a)     #开头为数字所以返回none                          b = re.match('\D\D','*es12testasdtest')  print(b)   #返回*eprint(re.match('\s',' 23es 12testasdtest'))   #匹配空格print(re.match('\s','   23es 12testasdtest')) #匹配tabprint(re.match('\s','\r23es 12testasdtest')) #匹配\r换行print(re.match('\s','23es 12testasdtest')) #返回noneprint(re.match('\S',' 23es 12testasdtest'))   #返回noneprint(re.match('\S','\r23es 12testasdtest'))   #noneprint(re.match('\S','23es 12testasdtest'))  print(re.match('\w','23es 12testasdtest'))   #返回noneprint(re.match('\w\w\w','aA_3es 12testasdtest'))   #返回noneprint(re.match('\w\w\w','\n12testasdtest'))   #返回noneprint(re.match('\W','23es 12testasdtest'))   #返回noneprint(re.match('\W',' 23es 12testasdtest'))   #匹配空格print(re.match('12[234]','232s12testasdtest'))  #因为开头的12没匹配上,所以直接返回noneprint(re.match('12[234]','1232s12testasdtest')) #返回123print(re.match('12[^234]','232s12testasdtest'))  #因为开头的12没匹配上,所以直接返回noneprint(re.match('12[^234]','1232s12testasdtest')) #返回noneprint(re.match('12[^234]','1252s12testasdtest')) #返回125print(re.match('12[1-3a-c]','1232b12testasdtest'))  #123print(re.match('12[1-3a-c]','12b2b12testasdtest'))  #12bprint(re.match('12[1-3a-c]','12s2b12testasdtest'))  #返回none

表示数量

* 出现0次或无数次

+ 至少出现一次

? 1次或则0次

{m}指定出现m次

{m,} 至少出现m次

{m,n} 指定从m-n次的范围

代码演示 

python">a = re.match('..','testasdtest')  
print(a.group())   #输出te                             
a = re.match('.*','testasdtest')  
print(a.group())   #全部输出
print(re.match('a*','aatestasdtest')) #匹配跟随在字母a后面的所有a字符
print(re.match('\d*','23aatestasdtest')) #匹配前面为数字的字符
print(re.match('a\d*','ad23aatestasdtest')) #输出a, 因为*也可以代表0次
print(re.match('a+','aaatestasdtest')) #匹配前面为字母a的字符,且a至少有1一个
print(re.match('a+','atestasdtest'))   #a
print(re.match('a+','caaatestasdtest'))  #none
print(re.match('a?','abatestasdtest')) #匹配a出现0次或者1次数
print(re.match('a?','batestasdtest'))  #输出空,因为a可以为0次
print(re.match('a?','aaatestasdtest')) #a出现0次或者1次,输出1个a
print(re.match('to{3}','toooooabatestasdtest')) #匹配t以及跟随在后面的三个ooo
print(re.match('to{3}','tooabatestasdtest')) #只有两个0,返回none
print(re.match('to{3,}','toooooabatestasdtest')) #匹配t以及跟随在后面的三个ooo至少出现3次
print(re.match('to{3,}','tooabatestasdtest')) #只有两个0,返回none
print(re.match('to{3,4}','toooabatestasdtest')) #刚好有三个ooo,成功匹配
print(re.match('to{3,4}','tooabatestasdtest'))  #只有两个o,返回none
print(re.match('to{3,4}','toooooabatestasdtest')) #提取最多四个o

 匹配边界

$ 匹配结尾字符

^ 匹配开头字符         

\b 匹配一个单词的边界

\B 匹配非单词边界

 代码演示

python">print(re.match('.*d$','2testaabcd')) #字符串必须以d结尾 
print(re.match('.*c','2testaabcd'))  #字符串不是以c结尾,返回none
print(re.match('^2','2stoooabatestas')) #规定必须以2开头,否则none 
print(re.match('^2s','2stoooabatestas')) #必须以2s开头
print(re.match(r'.*ve\b','ve.2testaabcd'))  #因为在python中\代表转义,所以前面加上r消除转义
print(re.match(r'.*ve\b','ve2testaabcd'))
print(re.match(r'.*ve\B','2testaavebcdve'))  #ve的右边需要有字母或者数字 
print(re.match(r'.*ve\B','2testaave3bcdve'))

 匹配分组

| 匹配左右任意一个表达式

(ab) 将括号中字符作为一个分组

python">print(re.match(r'\d[1-9]|\D[a-z]','2233')) #匹配|两边任意一个表达式
print(re.match(r'\d[1-9]|\D[a-z]','as')) 
a = re.match(r'<h1>(.*)<h1>','<h1>你好啊<h1>')
print(a.group())#输出匹配的字符
print(a.groups())#会将()中的内容会作为一个元组字符装在元组中
print('`````````````')
b = re.match(r'<h1>(.*)(<h1>)','<h1>你好啊<h1>')
print(b.groups()) #有两括号就分为两个元组元素
print(b.group(0))#group中默认是0
print(b.group(1)) #你好啊
print(b.group(2)) #h1

2、findall

从字面意思上就可以看到,findall是寻找所有能匹配到的字符,并以列表的方式返回

re.s

findall中另外一个属性re.S

在字符串a中,包含换行符\n,在这种情况下

  • 如果不使用re.S参数,则只在每一行内进行匹配,如果一行没有,就换下一行重新开始。
  • 而使用re.S参数以后,正则表达式会将这个字符串作为一个整体,在整体中进行匹配。

 如下要寻找test.*123的数据,因为test和123在不同的行,如果没加re.s的话,他会在每一个进行匹配查找而不是将字符串作为一个整体进行查找

python">
import re
a = """aaatestaa     
aaaa123"""
print(re.findall(r'test.*123',a))       
print(re.findall(r'test.*123',a,re.S))

 

3、sub


查找字符串中所有相匹配的数据进行替换

sub(要替换的数据,替换成什么,要替换的数据所在的数据)

python">import re
print(re.sub('php','python','php是世界上最好的语言——php'))  
#输出 "python是世界上最好的语言——python"


4、split


对字符串进行分割,并返回一个列表

python">import re
s = "itcase,java:php-php3;html"
print(re.split(r",",s))           #以,号进行分割
print(re.split(r",|:|-|;",s))     #以,或者:或者-或者;进行分割
print(re.split(r",|:|-|%",s))    #找不到的分隔符就忽略


http://www.ppmy.cn/news/1501260.html

相关文章

14. Hibernate 一对多双向关联映射

1. 前言 本节课程和大家一起聊聊一对多关联映射。通过本节课程&#xff0c;你将了解到&#xff1a; 如何实现一对多关联映射&#xff1b; 如何实现双向一对多关联映射&#xff1b; 关联映射中的级联操作。 2. 一对多关联映射 关系型数据库中表与表中的数据存在一对多&…

【FFmpeg】avcodec_receive_packet函数

目录 1.avcodec_receive_packet FFmpeg相关记录&#xff1a; 示例工程&#xff1a; 【FFmpeg】调用ffmpeg库实现264软编 【FFmpeg】调用ffmpeg库实现264软解 【FFmpeg】调用ffmpeg库进行RTMP推流和拉流 【FFmpeg】调用ffmpeg库进行SDL2解码后渲染 流程分析&#xff1a; 【FFm…

帆软报表学习官网 中文

帆软报表学习官网&#xff1a;快速入门指南- FineReport帮助文档 - 全面的报表使用教程和学习资料

前后端分离的开发模式+YAPI接口文档

博客主页&#xff1a;音符犹如代码系列专栏&#xff1a;JavaWeb关注博主&#xff0c;后期持续更新系列文章如果有错误感谢请大家批评指出&#xff0c;及时修改感谢大家点赞&#x1f44d;收藏⭐评论✍ 早期的开发模式&#xff1a;前后端混合开发 在这种模式下开发下&#xff0c;…

C++笔记---缺省参数和函数重载

1. 缺省参数 1.1 定义 缺省参数是声明或定义函数时为函数的参数指定一个缺省值&#xff08;默认值&#xff09;。在调用该函数时&#xff0c;如果没有指定实参 则采用该形参的缺省值&#xff0c;否则使用指定的实参&#xff0c;缺省参数分为全缺省和半缺省参数。 void Func(…

C语言100基础拔高题(3)

1.利用递归函数调用方式&#xff0c;将所输入的5个字符&#xff0c;以相反顺序打印出来。 解题思路&#xff1a;通过反复调用一个打印最后一个元素的函数&#xff0c;来实现此功能。源代码如下: #include<stdio.h> void oposize(char str[], int len); int main() {//利…

@Transactional使用的注意事项

在项目中涉及到CRUD操作时&#xff0c;一般都会在方法上添加该注解&#xff0c;以为加上Transactional&#xff0c;Spring就可以自动帮我们进行事务的开启、提交 有一个很多人都会犯的误区&#xff1a; 将Spring事务与Transactional划上了等号&#xff0c;只要有数据库相关操作…