Stax
Tools

Regex Cheat Sheet

Quick reference for regular expressions: anchors, character classes, quantifiers, groups, lookarounds, flags, and ready-to-use patterns.

Anchors

PatternMatches
^Start of string (or line with m flag)
$End of string (or line with m flag)
\bWord boundary (between \w and non-word)
\BNon-word boundary
\A / \ZStart / end of string (PCRE only — not JS)

Character classes

PatternMatches
.Any character except newline (or any with s flag)
\dDigit [0-9]
\DNon-digit
\wWord char [A-Za-z0-9_]
\WNon-word char
\sWhitespace (space, tab, newline)
\SNon-whitespace
[abc]Any of a, b, or c
[^abc]Anything except a, b, or c
[a-z]Range a through z
[a-zA-Z0-9]Multiple ranges

Quantifiers

PatternMatches
*0 or more (greedy)
+1 or more (greedy)
?0 or 1 (optional)
{3}Exactly 3
{2,5}2 to 5
{2,}2 or more
*? +? ??Lazy (match as little as possible)
*+ ++ ?+Possessive (no backtracking — PCRE)

Groups & references

PatternMatches
(abc)Capture group 1
(?:abc)Non-capturing group
(?<name>abc)Named group (use \k<name> to backreference)
\1 \2Backreference to group 1, 2
a|ba or b (alternation)

Lookarounds

PatternMatches
(?=abc)Followed by abc (positive lookahead)
(?!abc)NOT followed by abc (negative lookahead)
(?<=abc)Preceded by abc (positive lookbehind)
(?<!abc)NOT preceded by abc (negative lookbehind)

Flags

FlagEffect
gGlobal — find all matches, not just first
iCase-insensitive
mMultiline — ^ and $ match line boundaries
sDotall — . matches newline too
uUnicode mode
ySticky (JavaScript) — match at lastIndex only

Common ready-to-use patterns

Use casePattern
Email (basic)^[\w.+-]+@[\w-]+\.[\w.-]+$
URL (http/https)^https?://[^\s/$.?#].[^\s]*$
IPv4^(\d{1,3}\.){3}\d{1,3}$
Indian phone^(\+91[\-\s]?)?[0]?(91)?[6-9]\d{9}$
UUID v4^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$
ISO 8601 date^\d{4}-\d{2}-\d{2}$
Hex color^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$
Slug (URL-friendly)^[a-z0-9]+(?:-[a-z0-9]+)*$
PAN (India)^[A-Z]{5}[0-9]{4}[A-Z]$
GSTIN (India)^\d{2}[A-Z]{5}\d{4}[A-Z]\d[Z]\d$

Test any pattern interactively in our Regex Tester.

正規表現チートシート——すぐに使えるパターンを含む完全な正規表現リファレンス

正規表現はソフトウェア開発において最もレバレッジの高いスキルの一つです——適切に書かれた1つの正規表現で50行の文字列解析コードを置き換えられます。しかし多くの開発者は正規表現ができることのごく一部しか使っておらず、複雑なバリデーション、抽出、変換タスクを積み残したまま基本的なパターンに頼りがちです。このチートシートは正規表現ツールキットを網羅しています:アンカー、文字クラス、量指定子、グループ、ルックアラウンド、フラグ、そしてPAN番号やGSTINなどインド固有の形式を含む一般的なバリデーションタスク向けのすぐに使えるパターンライブラリ。

問題に一致するセクションをスキャンしてください——アンカー、文字クラス、量指定子、グループ、またはルックアラウンド。パターンをコピーして、正規表現テスターで実際の入力に対して動作を確認します。このリファレンスのパターンはJavaScriptとPCREフレーバーで動作します。フレーバー間で違いがある場合(可変長のルックビハインドのサポートや所有量指定子など)は、備考欄に記載されています。下部の「すぐに使える一般的なパターン」セクションはコピー&ペーストライブラリです——メールのバリデーション、URLマッチング、IPv4、インドの電話番号、UUID、ISO日付、HEXカラー、URLスラグ、PAN番号、GSTINのパターンはすべてテスト済みで使用可能です。

アンカー(^ $ \b)はパターンが文字列のどこでマッチングを許可するかを制御します——アンカーがないと、パターンは部分的なマッチングを含めて入力のどこでもマッチします。文字クラス(\d \w \s . [abc] [^abc])は各位置で許可される文字を定義します。量指定子(* + ? {n,m}とその遅延バリアント)は文字またはグループが何回繰り返せるかを制御します。グループと選択((abc) (?:abc) a|b)はシーケンスを単位としてマッチし、抽出のためにサブストリングをキャプチャし、複数の有効なパターンの代替を提供します。正規表現の他のすべて——ルックアラウンド、後方参照、フラグ——はこの4つの基盤の上に構築されています。これらをマスターすれば、必要な正規表現を何でも構築できます。

フォーム入力をバリデートするバックエンド開発者は、サーバーサイドコードに埋め込む前にメール、電話番号、郵便番号、ID形式のバリデーションパターンを見つけたり確認したりするために使用します。ETLパイプラインを書くデータエンジニアは、ログデータ、CSVファイル、APIレスポンスのテキスト抽出と変換パターンに使用します。セキュリティ研究者はWAFルールを書いたり、侵入検知のためにログパターンを分析したりする際に使用します。フォームバリデーションを構築するフロントエンド開発者はすぐに使えるパターンセクションを出発点として使用し、そこからカスタマイズします。インドのアプリに携わる開発者は、他では正確に記述されているものを見つけにくいPAN番号やGSTINのバリデーションパターンに特に使用します。

これは静的なリファレンスページです——データの収集や送信は行われません。リンクされている正規表現テスターを使用して自分のデータに対してパターンをテストしてください。そちらもすべてブラウザ内で動作します。

よくある質問

How do I learn regex fast?
Memorize the four pillars: anchors (^ $ \b), character classes (\d \w \s . []), quantifiers (* + ? {n,m}), and groups (). Everything else builds on these. Test every pattern as you learn it — rote memorization without practice fails fast. Spend 30 minutes a day for two weeks and you'll cover 95% of real-world cases.
What's the difference between PCRE, JavaScript, and POSIX regex?
PCRE (Perl-compatible) is the richest dialect — supports lookbehind, named groups, recursion. JavaScript regex is similar to PCRE but lacks variable-length lookbehind in older browsers. POSIX is older, mostly used in grep/sed/awk; lacks lookarounds entirely. Most online regex testers default to PCRE or JavaScript flavor.
When should I NOT use regex?
Parsing HTML, XML, or JSON — use proper parsers. Matching balanced brackets/parens — regex can't count. Validating email addresses for delivery — use proper email validation libraries that check MX records. The 'if it can be parsed' rule: if the language has a defined grammar, use a parser, not regex.
Why is .* so slow on long input?
It's catastrophic backtracking. Greedy quantifiers like .* try every possible match before giving up. On long inputs with nested quantifiers (e.g., (a+)+), regex can take exponential time. Solutions: use atomic groups (?>...), possessive quantifiers (*+, ++), or anchor with lookarounds. Re-write to avoid nested optionality.
How do I match a literal special character?
Escape it with a backslash. Special chars in regex: . * + ? ^ $ ( ) [ ] { } | \ /. To match a literal dot: \. To match a literal backslash: \\. Inside character classes [] most special chars lose meaning except - ] \ ^. So [.] matches a literal dot without escaping needed inside the class.

関連ツール