当前位置:网站首页>正则表达式-re模块
正则表达式-re模块
2022-08-09 12:51:00 【某x?】
re模块
re模块是python独有的匹配字符串的模块,但是很多功能是基于正则表达式实现的,所以re模块多用于正则匹配
re标志位 | 作用 |
---|---|
re.I | 忽略大小写 |
re.L | 让\w,\W,\b,\B和区分大小写的匹配取决于当前的语言环境。该标志只能与字节模式一起使用。不建议使用此标志,因为语言环境机制非常不可靠,它一次只能处理一种“区域性”,并且仅适用于8位语言环境。默认情况下,Python 3中已为Unicode(str)模式启用了Unicode匹配,并且能够处理不同的语言环境/语言。 |
re.M | 多行模式 |
re.S | 使’.‘特殊字符与任何字符都匹配,包括换行符;没有此标志,’.'将匹配除换行符以外的任何内容。 |
常用方法
re.compile(pattern,flags=0)
将正则表达式模式编译为正则表达式对象,可使用match、search等方法进行匹配
# 生成正则对象 prog = re.compile('\d{2}') prog.search('12abc') prog.match('123abc') prog.search('12abc').group() # 通过调用group()方法得到匹配的字符串,如果字符串没有匹配,则返回None。match也一样 # 输出 <_sre.SRE_Match object; span=(0, 2), match='12'> <_sre.SRE_Match object; span=(0, 2), match='12'> '12'
re.search(pattern,string,flags)
扫描字符串以查找正则表达模式产生匹配项的第一个位置,然后返回match对象.
re.search('\w+','abcde').group() # 'abcde'
re.match(pattern,string,flags=0)
如果字符串开头的零个或多个字符与正则表达式模式匹配,则返回相应的匹配对象。
re.match('a','abcade').group() # 'a'
re.findall(pattern,string,flags=0)
在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表。注意**:** match 和 search 是匹配一次 findall 匹配所有。
import re ret = re.findall(r"\d+", "python = 9999, c = 7890, c++ = 12345") print(ret) # ['9999', '7890', '12345']
re.finditer()
和 findall 类似,在字符串中找到正则表达式所匹配的所有子串,并把它们作为一个迭代器返回。
import re it = re.finditer(r"\d+", "12a32bc43jf3") for match in it: print(match.group())
re.sub(pattern,repl,string,count=0,flags=0)
sub是substitute的所写,表示替换,将匹配到的数据进⾏替换。
参数 描述 pattern 必选,表示正则中的模式字符串 repl 必选,要替换的字符串,也可以为一个函数 string 必选,被替换的字符串 count 可选,要替换的最大次数,如果省略或为0,则全部被替换 flag 标志位 # 替换为字符串 import re ret = re.sub(r"\d+", '998', "python = 997") print(ret) # 替换为函数 import re def add(temp): #int()参数必须是字符串,类似字节的对象或数字,而不是“re.Match” strNum = temp.group() num = int(strNum) + 1 return str(num) ret = re.sub(r"\d+", add, "python = 997") print(ret) ret = re.sub(r"\d+", add, "python = 99") print(ret)
re.subn()
跟sub函数系统,但是返回一个元组
边栏推荐
- JVM常用监控工具解释以及使用
- Compensation transaction and idempotency guarantee based on CAP components
- ARM板卡增加路由功能
- 5G 联通网管设计思路
- Extract EventBus encapsulation to base class using annotations
- Flutter Getting Started and Advanced Tour (3) Text Widgets
- 第六届”蓝帽杯“全国大学生网络安全技能大赛 半决赛
- 面试题精选:神奇的斐波那契数列
- Redis源码剖析之robj(redisObject)
- 在已打开图片上加水印(文字)
猜你喜欢
Compensation transaction and idempotency guarantee based on CAP components
WSA工具箱安装应用商店提示无法工作怎么解决?
kustomize入门示例及基本语法使用说明
基于 R 语言的判别分析介绍与实践 LDA和QDA
工作任务统计
Uni - app - uview Swiper shuffling figure component, click on the links to jump (click to get the item after the row data, remove data operation)
第六届“强网杯”全国网络安全挑战赛
5G China unicom repeater network management protocol real-time requirements
2022年非一线IT行业就业前景?
kustomize entry example and basic syntax instructions
随机推荐
Use RecyclerView to implement three-level collapsed list
技术分享 | 接口自动化测试如何处理 Header cookie
Clock frequency and baud rate count for serial communication in FPGA
ftplib+ tqdm 上传下载进度条
腾讯发布第二代四足机器人Max,梅花桩上完成跳跃、空翻
Intra-group reverse order adjustment of K nodes
透明tune proxy
绘制混合密度函数图以及添加分位数线
万物皆可柯里化的 Ramda.js
Customize VIEW to realize in-app message reminder to rotate up and down
第六届”蓝帽杯“全国大学生网络安全技能大赛 半决赛
剑指offer,剪绳子2
Rust from entry to proficient 04 - data types
Simple encapsulation of glide tool class
Standing wave ratio calculation method
5G China unicom general exception handling
工作任务统计
在已打开图片上加水印(文字)
史上最猛“员工”,疯狂吐槽亿万富翁老板小扎:那么有钱,还总穿着同样的衣服!...
JVM之配置介绍(一)