您现在的位置是:网站首页> 编程资料编程资料
Python正则表达式re模块详解(建议收藏!)_python_
2023-05-26
427人已围观
简介 Python正则表达式re模块详解(建议收藏!)_python_
前言
正则表达式是对字符串提取的一套规则,我们把这个规则用正则里面的特定语法表达出来,去匹配满足这个规则的字符串。正则表达式具有通用型,不仅python里面可以用,其他的语言也一样适用。
python中re模块提供了正则表达式的功能,常用的有四个方法(match、search、findall)都可以用于匹配字符串
match
匹配字符串
re.match()必须从字符串开头匹配!match方法尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。主要参数如下:
re.match(pattern, string) # pattern 匹配的正则表达式 # string 要匹配的字符串
例子
import re a = re.match('test','testasdtest') print(a) #返回一个匹配对象 print(a.group()) #返回test,获取不到则报错 print(a.span()) #返回匹配结果的位置,左闭右开区间 print(re.match('test','atestasdtest')) #返回None
从例子中我们可以看出,re.match()方法返回一个匹配的对象,而不是匹配的内容。如果需要返回内容则需要调用group()。通过调用span()可以获得匹配结果的位置。而如果从起始位置开始没有匹配成功,即便其他部分包含需要匹配的内容,re.match()也会返回None。
单字符匹配
以下字符,都匹配单个字符数据。且开头(从字符串0位置开始)没匹配到,即使字符串其他部分包含需要匹配的内容,.match也会返回none

. 匹配任意一个字符
使用几个点号就代表几个字符
import re a = re.match('..','testasdtest') print(a.group()) #输出te b = re.match('ab.','testasdtest') print(b) #返回none,因为表达式是以固定的ab开头然后跟上通配符. 所以必须要先匹配上ab才会往后进行匹配![]()
\d 匹配数字
一个\d代表一个数字。开头没匹配到,即使字符串其他部分包含需要匹配的内容,.match也会返回none
import re 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
\D 匹配非数字
开头没匹配到,即使字符串其他部分包含需要匹配的内容,.match也会返回none
import re a = re.match('\D','23es12testasdtest') print(a) #开头为数字所以返回none b = re.match('\D\D','*es12testasdtest') print(b) #返回*e\s 匹配特殊字符,如空白,空格,tab等
import re 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
\S 匹配非空白
import re print(re.match('\S',' 23es 12testasdtest')) #返回none print(re.match('\S','\r23es 12testasdtest')) #none print(re.match('\S','23es 12testasdtest')) 
\w 匹配单词、字符,如大小写字母,数字,_ 下划线
import re 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
\W 匹配非单词字符
import re print(re.match('\W','23es 12testasdtest')) #返回none print(re.match('\W',' 23es 12testasdtest')) #匹配空格![]()
[ ] 匹配[ ]中列举的字符
只允许出现[ ]中列举的字符
import re print(re.match('12[234]','232s12testasdtest')) #因为开头的12没匹配上,所以直接返回none print(re.match('12[234]','1232s12testasdtest')) #返回123
[^2345] 不匹配2345中的任意一个
import re print(re.match('12[^234]','232s12testasdtest')) #因为开头的12没匹配上,所以直接返回none print(re.match('12[^234]','1232s12testasdtest')) #返回none print(re.match('12[^234]','1252s12testasdtest')) #返回125[a-z3-5] 匹配a-z或者3-5中的字符
import re 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次或无数次
import re a = re.match('..','testasdtest') print(a.group()) #输出te a = re.match('.*','testasdtest') print(a.group()) #全部输出![]()
import re print(re.match('a*','aatestasdtest')) #匹配跟随在字母a后面的所有a字符 print(re.match('\d*','23aatestasdtest')) #匹配前面为数字的字符 print(re.match('a\d*','ad23aatestasdtest')) #输出a, 因为*也可以代表0次
+ 至少出现一次
import re print(re.match('a+','aaatestasdtest')) #匹配前面为字母a的字符,且a至少有1一个 print(re.match('a+','atestasdtest')) #a print(re.match('a+','caaatestasdtest')) #none
? 1次或则0次
import re 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
{m}指定出现m次
import re print(re.match('to{3}','toooooabatestasdtest')) #匹配t以及跟随在后面的三个ooo print(re.match('to{3}','tooabatestasdtest')) #只有两个0,返回none![]()
{m,} 至少出现m次
import re print(re.match('to{3}','toooooabatestasdtest')) #匹配t以及跟随在后面的三个ooo print(re.match('to{3}','tooabatestasdtest')) #只有两个0,返回none提示:
本文由神整理自网络,如有侵权请联系本站删除!
本站声明:
1、本站所有资源均来源于互联网,不保证100%完整、不提供任何技术支持;
2、本站所发布的文章以及附件仅限用于学习和研究目的;不得将用于商业或者非法用途;否则由此产生的法律后果,本站概不负责!
相关内容
- python实现读取excel表格详解方法_python_
- Django超详细讲解图书管理系统的实现_python_
- 关于pygame自定义窗口创建及相关操作指南_python_
- 教你使用Python的pygame模块实现拼图游戏_python_
- Python海象运算符的用法教程_python_
- pygame学习笔记之设置字体及显示中文_python_
- 详解Python中matplotlib模块的绘图方式_python_
- pytest多文件执行顺序控制详解_python_
- Python+matplotlib绘制多子图的方法详解_python_
- Python服务器创建虚拟环境跑代码_python_
