Python的正则表达式
创始人
2024-12-01 09:36:23
0次

    学完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

结果:


test
(0, 4)
None

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

匹配单字符

. 匹配任意一个字符

\d 匹配数字

\D 匹配非数字

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

\S 匹配非空白

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

\W 匹配非单词字符

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

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

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

演示代码如下

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)   #要求匹配三个数字,匹配不到返回none  c = re.match('\d','es12testasdtest')    print(c)   #起始位置没有匹配成功,一样返回none  a = re.match('\D','23es12testasdtest')    print(a)     #开头为数字所以返回none                            b = re.match('\D\D','*es12testasdtest')    print(b)   #返回*e  print(re.match('\s',' 23es 12testasdtest'))   #匹配空格  print(re.match('\s','   23es 12testasdtest')) #匹配tab  print(re.match('\s','\r23es 12testasdtest')) #匹配\r换行  print(re.match('\s','23es 12testasdtest')) #返回none  print(re.match('\S',' 23es 12testasdtest'))   #返回none  print(re.match('\S','\r23es 12testasdtest'))   #none  print(re.match('\S','23es 12testasdtest'))    print(re.match('\w','23es 12testasdtest'))   #返回none  print(re.match('\w\w\w','aA_3es 12testasdtest'))   #返回none  print(re.match('\w\w\w','\n12testasdtest'))   #返回none  print(re.match('\W','23es 12testasdtest'))   #返回none  print(re.match('\W',' 23es 12testasdtest'))   #匹配空格  print(re.match('12[234]','232s12testasdtest'))  #因为开头的12没匹配上,所以直接返回none  print(re.match('12[234]','1232s12testasdtest')) #返回123  print(re.match('12[^234]','232s12testasdtest'))  #因为开头的12没匹配上,所以直接返回none  print(re.match('12[^234]','1232s12testasdtest')) #返回none  print(re.match('12[^234]','1252s12testasdtest')) #返回125    print(re.match('12[1-3a-c]','1232b12testasdtest'))  #123  print(re.match('12[1-3a-c]','12b2b12testasdtest'))  #12b  print(re.match('12[1-3a-c]','12s2b12testasdtest'))  #返回none

表示数量

* 出现0次或无数次

+ 至少出现一次

? 1次或则0次

{m}指定出现m次

{m,} 至少出现m次

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

代码演示 

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 匹配非单词边界

 代码演示

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) 将括号中字符作为一个分组

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'

(.*)

','

你好啊

') print(a.group())#输出匹配的字符 print(a.groups())#会将()中的内容会作为一个元组字符装在元组中 print('`````````````') b = re.match(r'

(.*)(

)','

你好啊

') 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的话,他会在每一个进行匹配查找而不是将字符串作为一个整体进行查找

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

 

3、sub


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

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

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


4、split


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

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

相关内容

热门资讯

实测交流透视挂!宝玛大厅ai智... 实测交流透视挂!宝玛大厅ai智能透视挂教程,宝玛大厅破解软件经验(本来有挂辅助)1、每一步都需要思考...
总算了解辅助器!朱雀吴江三缺一... 总算了解辅助器!朱雀吴江三缺一ai智能透视辅助挂技巧,朱雀吴江三缺一好像是有挂(有辅助挂细节)朱雀吴...
今日公布透视!九五至尊大厅ai... 今日公布透视!九五至尊大厅ai智能透视插件,九五至尊大厅有没有透视挂窍门(切实有挂技术)1、在九五至...
玩家必看科普开挂!聚闲麻将ai... 玩家必看科普开挂!聚闲麻将ai智能透视辅助挂软件,聚闲麻将总是真的有挂(有辅助挂方式)聚闲麻将能透视...
据报道开挂!旅游娱乐ai智能开... 据报道开挂!旅游娱乐ai智能开挂工具,旅游娱乐辅助工具演示(一直有挂教学)1、全新机制【旅游娱乐ai...
一分钟揭秘开挂!决胜朋来麻将a... 一分钟揭秘开挂!决胜朋来麻将ai智能透视辅助挂教程,决胜朋来麻将真是真的是有挂(有辅助挂总结)1、首...
如何分辨真伪透视!红牛ai智能... 如何分辨真伪透视!红牛ai智能透视攻略,红牛是不是有挂经验(真是有挂技术)1、操作简单,无需红牛手机...
2分钟细说辅助器!江湖大庆麻将... 2分钟细说辅助器!江湖大庆麻将ai智能透视辅助挂攻略,江湖大庆麻将竟然是真的有挂(有辅助挂头条)1、...
总算清楚透视挂!老夫子厅ai智... 总算清楚透视挂!老夫子厅ai智能透视挂技巧,老夫子厅怎么开挂课程(好像发现有挂)老夫子厅破解侠是真的...
解密关于辅助!菜鸟麻将ai智能... 解密关于辅助!菜鸟麻将ai智能透视辅助挂脚本,菜鸟麻将总是存在有挂(有辅助挂存在)1、用户打开应用后...