当前位置:网站首页>正则表达式学习笔记
正则表达式学习笔记
2022-07-02 01:45:00 【stark张宇】
正则表达式是什么
校验数据的有效性、查找符合要求的文本以及对文本进行切割和替换等操作。
正则表达式的目的是真正的强大之处就在于可以查找符合某个规则的文本。
元字符
1.特殊字符串
- . 除换行符外任意字符
- \d 表示任意单个数字
- \w 表示任意单个数字或字母或下划线
- \s 表示任意单个空白符
\D \W \S,意思正相反。
2.空白符
- \r 回车符
- \n 换行符
- \f 换页符
- \t 制表符
- \v 垂直制表符
- \s 任意空白符
3.量词
- * 代表0次到多次
- + 1次到多次
- ? 0到1次
- {m} 出现m次
- {m,} 出现至少m次
- {m,n} m到n次
4.范围
- |或 ,如
ab|bc
代表ab或bc - […] 多选一,括号中任意单个元素
- [a-z] 匹配a-z之间任意单个元素
- [^…] 取反,不能包括括号中的任意单个元素
贪婪匹配、非贪婪匹配和独占模式
- 贪婪模式的特点就是尽可能进行最大长度匹配
- 非贪婪模式会尽可能短地去匹配。
- 独占模式和贪婪模式很像,独占模式会尽可能多地去匹配,如果匹配失败就结束,不会进行回溯,这样的话就比较节省时间。具体的方法就是在量词后面加上加号(+),例如
xy{1,3}+yz
注意: Python 和 Go 的标准库目前都不支持独占模式。
(“.+?”)|\w{2,6}
分组和引用
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uAMt23It-1656663240092)(https://note.youdao.com/yws/res/20143/WEBRESOURCEf8297d4f75fd7803bb138a4326c9a4fd)]
如图所示的正则,将日期和时间都括号括起来。这个正则中一共有两个分组,日期是第 1 个,时间是第 2 个。
1.不保存子组
分组的作用就是后续可能还会被引用到,但是如果不需要引用的时候可以添加不保存子组,表达式(?:xxx)
,优点是正则性能会更好,在子组计数时也更不容易出错。
2.括号嵌套
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Euot1k2Y-1656663240095)(https://note.youdao.com/yws/res/20156/WEBRESOURCEe2aeab108bee9c5908c7e6405867abd8)]
分组引用在查找中使用
…略
四种匹配模式
1.不区分大小写模式(Case-Insensitive)
不区分大小写模式,正则表达式(?i)cat
,这样匹配的字符就不区分大小写了
如果我们想要前面匹配上的结果,和第二次重复时的大小写一致,那该怎么做呢?我们只需要用括号把修饰符和正则 cat 部分括起来,加括号相当于作用范围的限定,让不区分大小写只作用于这个括号里的内容。正则表达式((?i)cat)
2.点号通配模式(Dot All)
元字符里讲.
可以匹配除了换行以外的任何元素,用单行模式中使用.
,正则表示式(?s).
3.多行匹配模式
多行模式的表达式(?m)^...|...$
,这样的匹配好处是每一行
4.注释模式
略…
断言
断言是指对匹配到的文本位置有要求。
1.单词边界
在准确匹配单词时,我们使用 \b...\b
就可以实现了,也可以指定表达式的开头和结尾^...$
2.环视
环视的主要是定义清晰的边界。
(?<=Y)
,左边是Y(?<=!Y)
,左边不是Y(?=Y)
,右边是Y(?!Y)
,右边不是Y
左尖括号代表看左边,没有尖括号是看右边,感叹号是非的意思。
边栏推荐
- Have you stepped on the nine common pits in the e-commerce system?
- Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
- KS006基于SSM实现学生成绩管理系统
- error: . repo/manifests/: contains uncommitted changes
- Learning note 3 -- Key Technologies of high-precision map (Part 1)
- SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5
- uTools
- 1217 supermarket coin processor
- Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
- Modeling essays series 124 a simple coding method
猜你喜欢
Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
技术大佬准备就绪,话题C位由你决定
LeetCode、3无重复最长子序列
Data visualization in medical and healthcare applications
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
6-2漏洞利用-ftp不可避免的问题
Convolutional neural network (including code and corresponding diagram)
[image enhancement] vascular image enhancement based on frangi filter with matlab code
Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
Should enterprises choose server free computing?
随机推荐
分卷压缩,解压
Penser au jeu 15: penser au service complet et au sous - service
How can the tsingsee Qingxi platform play multiple videos at the same time on the same node?
Matlab uses audioread and sound to read and play WAV files
Parted command
This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
游戏思考15:全区全服和分区分服的思考
It's already 30. Can you learn programming from scratch?
10 minutes to get started quickly composition API (setup syntax sugar writing method)
What is AQS and its principle
GL Studio 5 安装与体验
There are spaces in the for loop variable in the shell -- IFS variable
Niuke - Huawei question bank (51~60)
np. Where and torch Where usage
技术大佬准备就绪,话题C位由你决定
Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
k线图形态这样记(口诀篇)
Game thinking 15: thinking about the whole region and sub region Services
Is the knowledge of University useless and outdated?
Feature extraction and detection 16 brisk feature detection and matching