当前位置:网站首页>正则表达式
正则表达式
2022-06-21 16:05:00 【轩渊】
正则表达式
regular expression
正则表达式是一种文本模式,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为"元字符")。
正则表达式使用单个字符串来描述、匹配一系列匹配某个句法规则的字符串。
**re模块使用
查找
search #只返回一个
match #只返回一个,从头匹配的才算
findall #返回字符串
finditer #返回Match迭代器
search,match返回的都是Match对象
m.group() 显示数据
m.group(1) 显示第一组
m.groups() 显示所有组 (1,2,3)组
findall返回结果数组
替换
sub #替换
subn #替换后,多少被替换
re.sub(r'123','456',text)
返回替换后的字符串
re.subn(r'123','456',text)
返回替换后的字符串,与替换了几个地方
分割
split
正则表示用什么分割,返回分割的结果列表
例子
text='sassas , sas ; sas'
re.split(r'\s*[,.;]\s*',text) #多个空格[,.;]多个空格
返回['sassas', 'sas', 'sas']
**语法规则
字符类别
| 匹配 | 正则 |
|---|---|
| a,b,c,1,2,3 | 常量,写什么是什么 |
| \d | 一个数字 |
| \D | 一个非数字字符 |
| \s | 一个空格 |
| \S | 一个非空格 |
| \w | 一个任意字母数字下划线 |
| [abc] | a,b,c任意一个字符 |
| [a-e] | a到e的任意一个 |
| [^a-d] | 取反,除了a,b,c,d的任意一个 |
| [\d] | 退格符(Backspace) |
| . | 通配符除\n 外的任意字符 |
量词(次数)
| 正则 | 匹配 |
|---|---|
| * | 0或多个 |
| + | 1或多个 |
| ? | 0或1个 |
| {2} | 2个 |
| {2,5} | 2到5 |
| {2,} | 最少2 |
| {,4} | 最多4 |
组合模式
多个模式组合
\d{
6}[a-z]{
6} #六个数字,6个a-z
| #表示两种模式都可以,如两个数字或两个字符 \d{2}|\w{2}
#分组,分组后可以组为单位使用量词
() #如(122){3} 匹配3次122
位置
| 正则 | 匹配 |
|---|---|
| ^ | 字符串开头 |
| \A | 字符串开头,忽略m标记 |
| $ | 字符串末 |
| \Z | 字符串末,忽略m标记 |
| \b | 在单词边界 |
| \B | 不在单词边界 |
| (?<=xxx)\d | 匹配 xxx之后 的 一个数字 |
| (?=xxx)\d | 匹配 xxx开始的第一个数字 |
| (?!XXX)\d | 匹配不是xxx开始的 一个数字 |
分组
太复杂加注释
r'(sa)(?#前面是匹配什么的)'
| 正则 | 匹配 |
|---|---|
| (…) | 一个组 |
| (?P) | 组名为Y的组 |
| (?:…) | 不捕获组 |
| \Y | 匹配第Y个匹配的组 |
| (?P=Y) | 匹配名为Y的组 |
| (?#) | 注释 |
标记
re=re.findall(r'sad',flag=re.I)
| 正则 | 匹配 |
|---|---|
| i | 忽略大小写 |
| m | ^ 和$匹配行首与行尾 |
| s | .匹配换行符 |
| x | 允许空格与注释 |
| \u | Unicode匹配 |
替换
| 正则 | 匹配 |
|---|---|
| \g<0> | 插入整段匹配 |
| \g | 插入匹配Y,Y可以是匹配的名字或数字 |
| \Y | 插入名为Y的组 |
字符串匹配
import re
#固定字符串
re.findall(r'123455',text)
r表示raw,原生字符串
#某一类
re.findall(r'\d',text)
\d \d+
修饰前面的字符#如 \d匹配一个数字,\d{4}匹配四个
#组合匹配
re.findall(r'\d{2}abc\d{5}',text)
#多种情况,
|划分情况,找123或456
re.findall(r'123|345')
#限定位置
^开始位置
$结束位置
#内部约束
w字符,()表示一组,(\1)表示跟第一组一样
text='abcabc5613111111'
re.findall(r'(\w{3})(\1)',text)
返回[('abc','abc'),('111','111')]
书写步骤
1.查找匹配字符
- 根据需求找合适的模式
- 用查找函数
2.分割字符串
- 观察被分割字符串分割模式
- split函数
3.替换
- 根据需求找合适的模式
- 使用替换函数
边栏推荐
- Niuke network: verify the IP address
- Android kotlin class delegation by, by lazy key
- Compose 中的附带效应
- Hairui technology completed the pre-A round of financing of tens of millions of yuan to build the first artificial intelligent distribution Internet of things in China
- 20 pygame模块制作一个跳跃的小球游戏
- The next stop of Intelligent Manufacturing: cloud native + edge computing two wheel drive
- xlrd寻找指定内容所在行与行内容
- Sword finger offer II 089 House theft / Sword finger offer II 090 Ring house theft
- From Beijing "moisten" to Chicago, engineer Baoyu "moisten" the secret of growth
- kotlin 注解声明与使用
猜你喜欢

南京大学 静态软件分析(static program analyzes)-- introduction 学习笔记

Set up your own website (11)

Jetpack Compose 的阶段

In the "roll out" era of Chinese games, how can small and medium-sized manufacturers solve the problem of going to sea?
![[graduation n-year series] the fourth year of graduation](/img/0a/e7d903dec475c54ba1277fe133963f.png)
[graduation n-year series] the fourth year of graduation

node服务器 res.end()中写中文,客户端中乱码问题的解决方法

QT5知识:字符串列表QStringListModel

Serialization and deserialization of binary tree

【mysql学习笔记19】多表查询

How can aggressive programmers improve R & D efficiency Live broadcast Preview
随机推荐
xlrd寻找指定内容所在行与行内容
Design and implementation of face verification system for floating population management
2022年第三届全国运筹学/数据、模型与决策课程教学研讨会通知
变量与指针
[MySQL learning notes 12] paging query
Still using xshell? Try this cool SSH terminal tool, which is very powerful!
go corn定时任务简单应用
自然科学的根本任务
QT knowledge: using the qgraphicspixmapitem class
IEC62133与EN62133有何区别?主要测试哪些项目?
SCAU软件工程基础
module.exports指向问题
Move Protocol Beta测试版再调整,扩大总奖池
Use picgo core and Alibaba cloud to automatically upload typera pictures
[MySQL learning notes 17] sorting out common functions
常见设置模式
PowerPoint tutorial, how to change page orientation and slide size in PowerPoint?
Many software companies are actually "jokes"
The beta version of move protocol is stable, and it is temporarily decided to expand the scale of the prize pool
compose 编程思想