正则表达式速查表 — 完整的正则语法参考
正则表达式语法的常用速查参考。按类别浏览,找到所需语法,点击即可复制。涵盖字符、量词、锚点、分组等。
字符
| Syntax | Description |
|---|---|
. | Any character except newline |
\d | Digit (0-9) |
\w | Word character (a-z, A-Z, 0-9, _) |
\s | Whitespace (space, tab, newline) |
\D | Non-digit |
\W | Non-word character |
\S | Non-whitespace |
量词
| Syntax | Description |
|---|---|
* | Zero or more |
+ | One or more |
? | Zero or one |
{n} | Exactly n times |
{n,} | n or more times |
{n,m} | Between n and m times |
*? | Zero or more (lazy) |
+? | One or more (lazy) |
锚点
| Syntax | Description |
|---|---|
^ | Start of string/line |
$ | End of string/line |
\b | Word boundary |
\B | Non-word boundary |
分组
| Syntax | Description |
|---|---|
(…) | Capturing group |
(?:…) | Non-capturing group |
(?=…) | Positive lookahead |
(?!…) | Negative lookahead |
(?<=…) | Positive lookbehind |
(?<!…) | Negative lookbehind |
(?<name>…) | Named capturing group |
字符类
| Syntax | Description |
|---|---|
[abc] | Match a, b, or c |
[^abc] | Match anything except a, b, or c |
[a-z] | Match a through z |
[a-zA-Z] | Match a-z or A-Z |
标志
| Syntax | Description |
|---|---|
g | Global (match all) |
i | Case-insensitive |
m | Multiline (^ and $ match lines) |
s | Dotall (. matches newline) |
u | Unicode support |
特殊
| Syntax | Description |
|---|---|
| | Alternation (OR) |
\ | Escape special character |
(…)\1 | Back-reference to group 1 |