Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

正则表达式 #3

Open
GRPdream opened this issue Apr 12, 2019 · 0 comments
Open

正则表达式 #3

GRPdream opened this issue Apr 12, 2019 · 0 comments

Comments

@GRPdream
Copy link
Owner

GRPdream commented Apr 12, 2019

元字符

元字符 描述 例子
. 匹配除换行符以外的任意字符 ".ar" => The car parked in the garage.
[ ] 字符类,匹配方括号中包含的任意字符。 "ar[.]" => A garage is a good place to park a car.
[^] 否定字符类。匹配方括号中不包含的任意字符 "[^c]ar" => The car parked in the garage.
* 匹配前面的子表达式零次或多次 "\s*cat\s*" => The fat cat sat on the cat.
+ 匹配前面的子表达式一次或多次 "c.+t" => The fat cat sat on the mat.
? 匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。 "[T]?he" => The car is parked in the garage.
{n,m} 花括号,匹配前面字符至少 n 次,但是不超过 m 次。 "[0-9]{2,3}" => The number was 9.9997 but we rounded it off to 10.0.
{n,} 花括号,匹配前面字符至少 n 次,没有上限。 "[0-9]{2,}" => The number was 9.9997 but we rounded it off to 10.0.
{n} 花括号,匹配前面字符 n 次。 "[0-9]{2,3}" => The number was 9.9997 but we rounded it off to 10.0.
{xyz} 字符组,按照确切的顺序匹配字符xyz。 "(c|g|p)ar" => The car is parked in the garage.
| 分支结构,匹配符号之前的字符或后面的字符。 "(T|t)he|car" => The car is parked in the garage.
\ 转义符,它可以还原元字符原来的含义 /\.css$/
^ 匹配行的开始 "^(T|t)he" => The car is parked in the garage.
$ 匹配行的结束 "(at.)$" => The fat cat sat on the mat.

简写字符集

简写 描述
. 匹配除换行符以外的任意字符
\w 匹配所有字母和数字的字符: [a-zA-Z0-9_]
\W 匹配非字母和数字的字符: [^\w]
\d 匹配数字: [0-9]
\D 匹配非数字: [^\d]
\s 匹配空格符: [\t\n\f\r\p{Z}]
\S 匹配非空格符: [^\s]

断言

符号 描述 实例
?= 正向先行断言 "(T|t)he(?=\sfat)" => The fat cat sat on the mat.(它会匹配所有后面跟有 空格+fat 的the或The)
?! 负向先行断言 "(T|t)he(?!\sfat)" => The fat cat sat on the mat.(它会匹配所有后面没有跟有 空格+fat 的the或The)
?<= 正向后行断言 "(?<=(T|t)he\s)(fat|mat)" => The fat cat sat on the mat.(它会匹配所有前面有 the或The+空格 的 fat或mat)
?<! 负向后行断言 "(?<!(T|t)he\s)(cat)" => The cat sat on cat.(它会匹配所有前面没有 the或The+空格 的 cat)

标记

标记 描述
i 不区分大小写: 将匹配设置为不区分大小写。
g 全局搜索: 搜索整个输入字符串中的所有匹配。
m 多行匹配: 会匹配输入字符串每一行。

常用正则表达式

  • 正整数 ^\d+$
  • 负整数 ^-\d+$
  • 电话号码 ^+?[\d\s]{3,}$
  • 小写字母 ^([a-z])*$
  • 大写字母 ^([A-Z])*$
  • 密码 ^(?=^.{6,}$)((?=.*[A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z]))^.*$

参考文章传送门:

什么是正则表达式 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant