| 構文 | 説明 |
|---|---|
. | 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、名前付きグループ、特定のフラグなどの機能はエンジンによって異なります。