当前位置:网站首页>正则表达式学习笔记
正则表达式学习笔记
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
左尖括号代表看左边,没有尖括号是看右边,感叹号是非的意思。
边栏推荐
- Learn about servlets
- I Brief introduction of radio energy transmission technology
- New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port
- uTools
- [Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
- 电子协会 C语言 1级 33 、奇偶数判断
- VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
- Android: how can golden nine and silver ten squeeze into the first-line big factories from small and medium-sized enterprises? The depth of interview questions in large factories
- Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
- [IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card
猜你喜欢

How can I batch produce the same title for the video?

Learning note 3 -- Key Technologies of high-precision map (Part 1)

SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5
![[image enhancement] vascular image enhancement based on frangi filter with matlab code](/img/b3/b4164fb7db8645f470180e352b5717.png)
[image enhancement] vascular image enhancement based on frangi filter with matlab code
![[rust web rokcet Series 1] Hello, world and get, post, put, delete](/img/d8/7dd5fe409d349a13128b6af554f952.jpg)
[rust web rokcet Series 1] Hello, world and get, post, put, delete

Learning note 24 - multi sensor post fusion technology

Liteos learning - first knowledge of development environment

【LeetCode 43】236. The nearest common ancestor of binary tree

浅浅了解Servlet

Using tabbar in wechat applet
随机推荐
Memorabilia of domestic database in June 2022
How to use a product to promote "brand thrill"?
Modeling essays series 124 a simple coding method
What is AQS and its principle
The author is more willing to regard industrial Internet as a concept much richer than consumer Internet
matlab 使用 audioread 、 sound 读取和播放 wav 文件
Fastadmin controls the length of fields in the table
I Brief introduction of radio energy transmission technology
Four basic strategies for migrating cloud computing workloads
No converter found for return value of type: class
Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
并发编程的三大核心问题
6-3 vulnerability exploitation SSH environment construction
KS006基于SSM实现学生成绩管理系统
基于SSM实现微博系统
[rust web rokcet Series 2] connect the database and add, delete, modify and check curd
Feature extraction and detection 16 brisk feature detection and matching
2022年6月国产数据库大事记
GL Studio 5 installation and experience
Learning notes 25 - multi sensor front fusion technology