原作者文章:github/learn-regex
图片来源:https://www.douban.com/group/topic/152808442/
正则表达式测试网站:https://regex101.com/
Author:xiaozhu_sai
本文章仅用于交流使用,请勿转载。
本人在学习正则表达式的时候,知识点过于混乱,决定制作一份笔记,大量参考github/learn-regex的内容,修改了一部分用于本人查找资料使用。
正则表达式笔记
1、基本匹配
the
匹配所有的the。规则:字母t开始,接着h,再是e。
2、元字符
正则表达式主要依赖于元字符。
元字符 | 匹配内容 |
---|---|
() | 字符集 |
. | 代表单个字符,任意,除换行符 |
[ ] | 匹配方括号内任意字符 |
[^ ] | 匹配除方括号内的其他任意字符 |
* | 匹配0 ~ n个前的字符 |
+ | 匹配1 ~ n个前的字符,即至少出现1次 |
? | 前的字符可有可无,即0次或1次 |
{n,m} | 匹配n ~ m个前的字符或字符集 |
^ | 从开始,开始匹配 |
$ | 从末端,开始匹配 |
|转义字符,匹配[ ] ( ) { } . * + ? ^ $ \ | |
2.1.
.
匹配任意单个字符,但不匹配换行符。
例如,表达式.ar
匹配一个任意字符后面跟着是a
和r
的字符串。
".ar" => The <strong>car </strong>xxx <strong>par</strong> ked in the <strong>gar age.
2.2[]
字符集
方括号内不关心顺序,同时也可以指定范围。
例如,[0-9a-zA-Z]和同一个字母大小写[Tt]。
方括号内的句号[.]
就是.
,例如"sai[.]“匹配"sai.”
2.3[^ ]否定字符集
例如,表达式[^c]ar 匹配一个后面跟着ar的除了c的任意字符。
"[^c]ar" => The car<strong>par</strong>ked in thehref="#learn-regex"><strong>gar</strong>age.
2.4重复次数*
+
?
2.4.1 *
大于0次
.*
组合是匹配所有的字符
例如,"[a-z]*"匹配所有小写字母开头的字符串。
例如,“cat"匹配文本中的"cat"字符串。但”\s*cat\s*“匹配可以匹配cat单词的前后空格,” cat "
2.4.2 +
至少出现1次及以上
例如,“c.+t” => The fat cat sat on the mat
2.4.3?
出现1次或者0次,不匹配2次以上的的字符串。
[T]?he" => The car is parked in the garage.
2.5{}
限定重复次数
量词,常用来限定1个字符或者1组字符集[] ()
例如, 表达式 [0-9]{2,3}
匹配最少 2 位最多 3 位 0~9 的数字。
"[0-9]{2,3}" => The number was 9.<strong>999</strong>7 but we rounded it off to <strong>10</strong>.0.
我们可以省略第二个参数。
例如,[0-9]{2,}
匹配至少两位 0~9 的数字。
"[0-9]{2,}" => The number was 9.<strong>9997</strong> but we rounded it off to <strong>10</strong>.0.
如果逗号也省略掉则表示重复固定的次数。
例如,[0-9]{3}
匹配3位数字
"[0-9]{3}" => The number was 9.<strong>999</strong>7 but we rounded it off to 10.0.
2.6 () |
特征标群和或运算符
例如,"(T|t)he" => 匹配The car is parked in the.
2.7锚点 ^ $
2.7.1 ^
号
用于匹配以^
开头的字符串
例如,^(T|t)he
匹配以 The
或 the
开头的字符串。
"(T|t)he" => <strong>The</strong> car is parked in <strong>the</strong> garage.
"^(T|t)he" => <strong>The</strong> car is parked in the garage.
2.7.2 $
号
用于匹配以$
结尾的字符串
例如,(at\.)$
匹配以 at.
结尾的字符串。
"(at\.)" => The fat c<strong>at.</strong> s<strong>at.</strong> on the m<strong>at.</strong>
"(at\.)$" => The fat cat. sat. on the m<strong>at.</strong>
3.简写字符集
单个字符集仅仅代表单个字符
大写字符集代表非
简写 | 字符集描述 |
---|---|
. | 代表单个字符,任意,除换行符 |
\w | 匹配所有字母数字下划线,等同于[a-zA-Z0-9_] |
\W | 匹配所有符号,例如空格,非字母数字下划线,等同于[^\w] |
\d | 匹配数字,[0-9] |
\D | 匹配非数字,[\d][0-9] |
\s | 匹配所有空格字符,[\t\n\f\r\p{Z}] |
\n | 换行符 |
\r | 回车符 |
\p | 匹配DOS行终止符,等同于[\n\r] |
4.零宽度断言(前后预查)
先行断言:匹配正文+条件,正文在前
后发断言:条件+匹配正文,正文在后
符号 | 描述 |
---|---|
?= | 正先行断言-存在 |
?! | 负先行断言-排除 |
?<= | 正后发断言-存在 |
?<! | 负后发断言-排除 |
4.1正先行断言(?=条件)
匹配的正文后跟(?=条件)
例如,匹配the或者The,并且指定条件后面跟着\s空格和"fat"或者"mat"。
"(T|t)he(?=\sfat|\smat)" => <strong>The</strong> fat cat sat on **the** mat.
4.2负先行断言(?!条件)
匹配正文后不符合"条件"(?!条件)
例如,匹配不跟\s空格和fat
的the
或The
。
"(T|t)he(?!\sfat)" => The fat cat sat on <strong>the</strong> mat.
4.3正后发断言(?<=条件)
例如,表达式 (?<=(T|t)he\s)(fat|mat)
匹配 fat
和 mat
,且其前跟着 The
或 the
。
"(?<=(T|t)he\s)(fat|mat)" => The <strong>fat</strong> cat sat on the <strong>mat</strong>.
4.4负后发断言(?<!条件)
例如,表达式 (?<!(T|t)he\s)(cat)
匹配 cat
,且其前不跟着 The
或 the
。
"(?<!(T|t)he\s)(cat)" => The cat sat on <strong>cat</strong>.
5.贪婪匹配和懒惰匹配
懒惰匹配.*?
贪婪匹配.*