tools
S
信頼度ランク
| S | 公式ソース確認済み |
| A | 成功実績多数・失敗例少数 |
| B | 賛否両論 |
| C | 動作未確認・セキュリティリスク高 |
| Z | 個人所感 |
正規表現:エンジニアが毎日使うパターン5選
メールアドレス・URL・日付・ログ解析・コードリファクタリングなど、実務で頻出する正規表現パターンを5つ厳選して解説します。
一言結論
正規表現は「完璧なバリデーション」を目指すと際限なく複雑になるため、メールは送信確認、URLは大まかな形式チェックと割り切り、名前付きキャプチャグループを使って可読性を保つことが実務での正しい使い方だ。
正規表現の基本記号(チートシート)
| 記号 | 意味 | 例 |
|---|---|---|
. | 任意の1文字 | a.c → abc, aXc |
* | 直前の要素が0回以上 | ab* → a, ab, abb |
+ | 直前の要素が1回以上 | ab+ → ab, abb(aはNG) |
? | 直前の要素が0or1回 | colou?r → color, colour |
^ | 行頭 | ^import |
$ | 行末 | \.ts$ |
\d | 数字 | \d{4} → 4桁の数字 |
\w | 英数字+アンダースコア | \w+ |
\s | 空白文字 | \s+ |
[...] | 文字クラス | [a-z0-9] |
(...) | グループ・キャプチャ | (https?) |
(?:...) | 非キャプチャグループ | (?:https?) |
パターン1: メールアドレス
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
emailRegex.test("user@example.com"); // true
emailRegex.test("user+tag@sub.co.jp"); // true
emailRegex.test("not-an-email"); // false
注意: 完璧なRFC 5322準拠の正規表現は非現実的に複雑。バリデーションはサーバー側で確認メール送信で行うのがベスト。
パターン2: URLの抽出
const urlRegex = /https?:\/\/[^\s<>"]+/g;
const text = "詳細は https://example.com/path?q=1 を参照。";
const urls = text.match(urlRegex);
// → ["https://example.com/path?q=1"]
// グループで分解
const detailed = /^(https?):\/\/([^\/]+)(\/[^\?#]*)?(\?[^#]*)?(#.*)?$/;
const m = "https://example.com/path?q=1#section".match(detailed);
// m[1]=https, m[2]=example.com, m[3]=/path, m[4]=?q=1, m[5]=#section
パターン3: 日付のフォーマット変換
// YYYY-MM-DD を YYYY年MM月DD日 に変換
const dateStr = "2026-04-08";
const formatted = dateStr.replace(
/^(\d{4})-(\d{2})-(\d{2})$/,
"$1年$2月$3日"
);
// → "2026年04月08日"
// ログから日時を抽出
const logLine = "[2026-04-08 14:30:22] ERROR: Connection refused";
const dateTimeRegex = /\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\]/;
const dt = logLine.match(dateTimeRegex)?.[1];
// → "2026-04-08 14:30:22"
パターン4: コードのリファクタリング(VS Code 検索・置換)
VS Code の「正規表現で検索」(Ctrl+H → .* ボタン)で活用。
# console.log を削除
検索: console\.log\(.*?\);?\n?
置換: (空欄)
# var を const に一括変換
検索: \bvar\b
置換: const
# 関数の引数に型を追加(TypeScript移行時)
検索: function (\w+)\((\w+)\)
置換: function $1($2: unknown)
# HTMLタグの属性を抽出
検索: <img[^>]+src="([^"]+)"
置換:  ← Markdown画像記法に変換
パターン5: ログ解析(named capture group)
// Nginxのアクセスログを解析
const nginxLog = '192.168.1.1 - - [08/Apr/2026:14:30:22 +0900] "GET /api/users HTTP/1.1" 200 1234';
const logRegex = /^(?<ip>\S+) \S+ \S+ \[(?<datetime>[^\]]+)\] "(?<method>\w+) (?<path>\S+) [^"]+" (?<status>\d+) (?<bytes>\d+)/;
const match = nginxLog.match(logRegex);
const { ip, status, path, method } = match!.groups!;
// ip="192.168.1.1", status="200", path="/api/users", method="GET"
// エラーレスポンスだけ抽出
const lines = logContent.split("\n");
const errors = lines.filter(line => /"\s+[45]\d{2}\s+/.test(line));
実用テクニック
先読み・後読み(Lookahead / Lookbehind)
// 数字の前に「¥」がある場合だけマッチ(¥はキャプチャしない)
const price = "¥1980 and 1980".match(/(?<=¥)\d+/g);
// → ["1980"](後読み: ¥の後ろの数字)
// passwordという文字に続く値を隠す
const masked = logLine.replace(/(?<=password=)\S+/, "***");
フラグ
/pattern/g // グローバル(全マッチ)
/pattern/i // 大文字小文字を無視
/pattern/m // 複数行(^ $ が各行に適用)
/pattern/s // . が改行にもマッチ
/pattern/u // Unicode モード(絵文字等)
参考: MDN 正規表現 / regex101.com(ブラウザ上でテスト可)