| 語法 | 描述 |
|---|---|
. | Any character except newline |
\d | Digit [0-9] |
\D | Non-digit [^0-9] |
\w | Word char [a-zA-Z0-9_] |
\W | Non-word character |
\s | Whitespace (space, tab, newline) |
\S | Non-whitespace |
[abc] | One of a, b, or c |
[^abc] | Not a, b, or c |
[a-z] | Character in range a–z |
[a-zA-Z] | Letter (any case) |
\p{L} | Any Unicode letter (u flag) |
| 語法 | 描述 |
|---|---|
^ | Start of string (or line with m flag) |
$ | End of string (or line with m flag) |
\b | Word boundary |
\B | Non-word boundary |
\A | Start of string (Python/Java) |
\Z | End of string (Python/Java) |
| 語法 | 描述 |
|---|---|
* | 0 or more (greedy) |
+ | 1 or more (greedy) |
? | 0 or 1 (optional) |
{n} | Exactly n times |
{n,} | n or more times |
{n,m} | Between n and m times |
*? | 0 or more (lazy) |
+? | 1 or more (lazy) |
?? | 0 or 1 (lazy) |
| 語法 | 描述 |
|---|---|
(abc) | Capturing group |
(?:abc) | Non-capturing group |
(?<name>abc) | Named capturing group |
\1 | Backreference to group 1 |
\k<name> | Named backreference |
(?|...) | Branch reset group (PCRE) |
| 語法 | 描述 |
|---|---|
(?=abc) | Positive lookahead — must be followed by abc |
(?!abc) | Negative lookahead — must NOT be followed by abc |
(?<=abc) | Positive lookbehind — must be preceded by abc |
(?<!abc) | Negative lookbehind — must NOT be preceded by abc |
| 標誌 | 名稱 | 描述 |
|---|---|---|
g | Global | Find all matches, not just the first |
i | Case-insensitive | Match regardless of case (A = a) |
m | Multiline | ^ and $ match start/end of each line |
s | Dotall | . matches newline characters too |
u | Unicode | Enable full Unicode support (\p{} classes) |
y | Sticky | Match only at lastIndex position (JS) |
x | Extended | Allow whitespace + comments in pattern (Python/PHP) |
| 模式 | Regex | 測試 |
|---|---|---|
[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,} | 在Regex測試器中嘗試 → | |
| URL | https?:\/\/[\w\-]+(\.[\w\-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=%]* | 在Regex測試器中嘗試 → |
| IPv4 Address | \b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b | 在Regex測試器中嘗試 → |
| Phone (US) | \+?1?[\s.-]?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4} | 在Regex測試器中嘗試 → |
| Date (YYYY-MM-DD) | \d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01]) | 在Regex測試器中嘗試 → |
| Time (HH:MM) | (?:[01]\d|2[0-3]):[0-5]\d | 在Regex測試器中嘗試 → |
| UUID | [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} | 在Regex測試器中嘗試 → |
| Hex Color | #(?:[0-9a-fA-F]{3}){1,2}\b | 在Regex測試器中嘗試 → |
| Slug | [a-z0-9]+(?:-[a-z0-9]+)* | 在Regex測試器中嘗試 → |
| ZIP Code (US) | \d{5}(?:-\d{4})? | 在Regex測試器中嘗試 → |
| Credit Card | (?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}) | 在Regex測試器中嘗試 → |
| HTML Tag | <([a-zA-Z][a-zA-Z0-9]*)(?:\s[^>]*)?\/?>.*?<\/\1> | 在Regex測試器中嘗試 → |
| Markdown Bold | \*\*([^*]+)\*\* | 在Regex測試器中嘗試 → |
| JWT Token | [A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+ | 在Regex測試器中嘗試 → |
| Semver | \bv?(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\w.]+)?\b | 在Regex測試器中嘗試 → |
| GitHub Username | (?<=github\.com\/)([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,37}[a-zA-Z0-9])?) | 在Regex測試器中嘗試 → |
| Positive Integer | ^[1-9]\d*$ | 在Regex測試器中嘗試 → |
| Float / Decimal | -?\d+(?:\.\d+)? | 在Regex測試器中嘗試 → |
| Blank Lines | ^\s*$ | 在Regex測試器中嘗試 → |
| Duplicate Words | \b(\w+)\s+\1\b | 在Regex測試器中嘗試 → |
正則表達式是定義搜尋模式的字元序列。用於字串搜尋、匹配和操作。Regex在JavaScript、Python、Java、PHP、Go和大多數現代程式語言中都有原生支援。
貪婪量詞(*, +, {n,m})盡可能多地匹配字元。惰性量詞(*?, +?, {n,m}?)盡可能少地匹配字元。
Lookahead(?=...)和lookbehind(?<=...)是零寬度斷言——它們在不消耗字元的情況下檢查模式。正向lookahead(?=foo)斷言'foo'在後面跟隨。
核心語法(字元類、量詞、錨點)在大多數語言中是一致的。但是,lookbehind、命名組和特定標誌等功能因引擎而異。