当前位置:网站首页>正则表达式学习笔记
正则表达式学习笔记
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
左尖括号代表看左边,没有尖括号是看右边,感叹号是非的意思。
边栏推荐
- 遊戲思考15:全區全服和分區分服的思考
- Based on configured schedule, the given trigger will never fire
- VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
- MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
- [Maya] the error of importing Maya into Metahuman
- Android: the kotlin language uses grendao3, a cross platform app development framework
- [IVX junior engineer training course 10 papers to get certificates] 0708 news page production
- Experimental reproduction of variable image compression with a scale hyperprior
- Edge computing accelerates live video scenes: clearer, smoother, and more real-time
- 开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?
猜你喜欢

Six lessons to be learned for the successful implementation of edge coding

遊戲思考15:全區全服和分區分服的思考

matlab 实现语音信号重采样和归一化,并播放比对效果

The role of artificial intelligence in network security

GL Studio 5 安装与体验

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

Unity AssetBundle subcontracting
![[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing](/img/56/87bc8fca9ceeab6484f567f7231fdb.png)
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing

LeetCode、3无重复最长子序列

2022年6月国产数据库大事记
随机推荐
There are spaces in the for loop variable in the shell -- IFS variable
MySQL application day02
Raspberry pie 4B learning notes - IO communication (1-wire)
卷積神經網絡(包含代碼與相應圖解)
卷积神经网络(包含代码与相应图解)
[rust web rokcet Series 1] Hello, world and get, post, put, delete
Matlab uses audioread and sound to read and play WAV files
The role of artificial intelligence in network security
Brief description of grafana of # yyds dry goods inventory # Prometheus
1218 square or round
Number of palindromes in C language (leetcode)
Should enterprises choose server free computing?
Docker installing Oracle_ 11g
Just using the way and method of consuming the Internet to land and practice the industrial Internet will not bring long-term development
Electronic Association C language level 1 33, odd even number judgment
Réseau neuronal convolutif (y compris le Code et l'illustration correspondante)
matlab 实现语音信号重采样和归一化,并播放比对效果
Learning note 24 - multi sensor post fusion technology
[IVX junior engineer training course 10 papers to get certificates] 09 chat room production
Learn basic K-line diagram knowledge in three minutes