-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
title: 正则表达式 | ||
date: 2023-12-06 | ||
--- | ||
|
||
![](https://raw.githubusercontent.com/smilelc3/blog/main/images/正则表达式/RegEx-Logo.webp) | ||
|
||
# 正则表达式 | ||
|
||
## 常用表达式语法 | ||
|
||
| 语法 | 描述 | 用例 | | ||
|:---:|:---|:---| | ||
| \\ | 将下一个字符标记为一个特殊字符、或一个原义字符、或一个向后引用、或一个进制转义符。| “n”匹配字符“n”。“\\n”匹配一个换行符。连续“\\\\”匹配“\”,而“\\(”匹配“(” | | ||
| ^ | 匹配输入字符串的**开始位置**| | | ||
| $ | 匹配输入字符串的**结束位置**| | | ||
| * | 匹配前面的子表达式**零次或多次**。*等价于{0,}| “zo*”能匹配“z”以及“zoo” | | ||
| + | 匹配前面的子表达式**一次或多次**。+等价于{1,} | “zo+”能匹配“zo”以及“zoo”,但不能匹配“z” | | ||
| ? | 匹配前面的子表达式**零次或一次**。?等价于{0,1} | “password(s)?”可以匹配“pawwsord”或“passwords” | | ||
| {n} | n是一个非负整数。**必须匹配n次** | “fo{2}d”不能匹配“fo”,但是能匹配“foo” | | ||
| {n,} | n是一个非负整数。**至少匹配n次** | “go{2,}d”不能匹配“god”,但是能匹配“good”和“gooood” | | ||
| {n,m} | m和n均为非负整数,其中n<=m。**最少匹配n次且最多匹配m次**,请注意在逗号和两个数之间不能有空格。 | | | ||
| ? | 当该字符紧跟在任何一个其他限制符(*,+,?, {n}, {n,}, {n,m})后面时,**匹配模式是非贪婪的**。非贪婪模式尽可能少的匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串| 对于字符串“oooo”,“o+?”将匹配单个“o”,而“o+”将匹配所有“o” | | ||
| . | 匹配除“\\n”之外的**任何单个字符**。要匹配包括“\\n”在内的任何字符,请使用像“(.\|\\n)”的模式。| | | ||
| (pattern) | 匹配pattern并获取这一匹配。所获取的匹配可以从产生的Matches集合得到,要匹配圆括号字符,请使用“`\(`” 或 “`\)`” | | | ||
| x\|y | **匹配x或y**,也可以多个一起使用“x\|y\|z” | “(b\|f)oo”能匹配“boo”或“foo”。“b\|foo”则匹配“b”或“foo” | | ||
| [xyz] | 匹配**所包含的任意一个字符** | “[abc]{2}”可以匹配“aa”或“ac”或“bb”等 | | ||
| [^xyz] | 匹配**未包含的任意一个字符** | | | ||
| [a-z] | 字符范围。匹配**指定范围内的任意字符** | “[a-z]+”可以匹配任意长度的小写字母字符串 | | ||
| \\d | 匹配**一个数字字符**。等价于[0-9] | | | ||
| \\D | 匹配**一个非数字字符**。等价于[^0-9] | | | ||
| \\n | 匹配**一个换行符**。等价于\\x0a | | | ||
| \\r | 匹配**一个回车符**。等价于\\x0d | | | ||
| \\s | 匹配**一个任何空白字符**,包括空格、制表符、换页符等等。等价于[\\f\\n\\r\\t\\v] | | | ||
| \\S | 匹配**一个任何非空白字符**,等价于[^\\f\\n\\r\\t\\v] | | | ||
| \\t | 匹配**一个制表符**。等价于\x09 | | | ||
| \\w | 匹配一个包括下划线的任何单词字符。等价于“[A-Za-z0-9_]” | | | ||
| \\W | 匹配任何非单词字符。等价于“[^A-Za-z0-9_]” | | | ||
| \\un| 匹配n,其中n是一个用四个十六进制数字表示的Unicode字符 | \\u00A9匹配版权符号(©),[\\u4e00-\\u9fa5]匹配中文字符 | |