チートシート
正規表現チートシート
正規表現の基本構文から応用メタ文字までを一目で確認できる実用リファレンス。
文字クラス
| 構文 | 説明 |
|---|---|
. | Any character except newline |
\d | Any digit (0-9) |
\D | Any non-digit |
\w | Word character (letter, digit, underscore) |
\W | Non-word character |
\s | Any whitespace (space, tab, newline) |
\S | Any non-whitespace |
[abc] | Any one of a, b, or c |
[^abc] | Any character except a, b, or c |
[a-z] | Any character in range a to z |
[0-9] | Any digit (same as \d) |
量指定子
| 構文 | 説明 |
|---|---|
* | Match 0 or more times (greedy) |
+ | Match 1 or more times (greedy) |
? | Match 0 or 1 time (optional) |
{n} | Match exactly n times |
{n,} | Match n or more times |
{n,m} | Match between n and m times |
*? | Match 0 or more times (lazy) |
+? | Match 1 or more times (lazy) |
?? | Match 0 or 1 time (lazy) |
アンカー・境界
| 構文 | 説明 |
|---|---|
^ | Start of string (or line with m flag) |
$ | End of string (or line with m flag) |
\b | Word boundary |
\B | Non-word boundary |
グループ・後方参照
| 構文 | 説明 |
|---|---|
(abc) | Capturing group |
(?:abc) | Non-capturing group |
(?<name>abc) | Named capturing group |
\1 | Backreference to group 1 |
\k<name> | Named backreference |
(a|b) | Alternation (OR) |
先読み・後読み (Lookaround)
| 構文 | 説明 |
|---|---|
(?=abc) | Positive lookahead — followed by abc |
(?!abc) | Negative lookahead — NOT followed by abc |
(?<=abc) | Positive lookbehind — preceded by abc |
(?<!abc) | Negative lookbehind — NOT preceded by abc |
フラグ
| 構文 | 説明 |
|---|---|
g | Global — find all matches |
i | Case-insensitive |
m | Multiline — ^ and $ match line boundaries |
s | Dotall — . matches newlines too |
u | Unicode mode |
v | Unicode sets (extended) |
d | Generate indices for matches |
y | Sticky — match from lastIndex only |
特殊文字・エスケープ
| 構文 | 説明 |
|---|---|
\n | Newline |
\r | Carriage return |
\t | Tab |
\0 | Null character |
\\ | Literal backslash |
\. | Literal dot |
\[ | Literal opening bracket |