コンテンツへスキップ
🛠️ToolsShed

Regex Cheat Sheet

ライブパターンテストとコピー用のインタラクティブな正規表現リファレンスです。

ライブテスター

Anchors

パターン説明
^Start of string / line
$End of string / line
\bWord boundary
\BNon-word boundary
\AStart of string only
\ZEnd of string only

Quantifiers

パターン説明
*0 or more times
+1 or more times
?0 or 1 time (optional)
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times
*?Lazy 0 or more
+?Lazy 1 or more

Character Classes

パターン説明
.Any character except newline
\dDigit [0-9]
\DNon-digit
\wWord char [a-zA-Z0-9_]
\WNon-word character
\sWhitespace
\SNon-whitespace
[abc]Character set — a, b, or c
[^abc]Negated set — not a, b, or c
[a-z]Character range

Groups & Alternation

パターン説明
(abc)Capturing group
(?:abc)Non-capturing group
(?<name>abc)Named capturing group
(?=abc)Positive lookahead
(?!abc)Negative lookahead
(?<=abc)Positive lookbehind
(?<!abc)Negative lookbehind
a|bAlternation — a or b

Flags

パターン説明
gGlobal — find all matches
iCase-insensitive
mMultiline — ^ and $ match line boundaries
sDotall — . matches newline too
uUnicode mode
ySticky — match at exact position

Common Patterns

パターン説明
^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$Email address
https?:\/\/[\w\-._~:/?#[\]@!$&'()*+,;=%]+URL (http/https)
^\+?[1-9]\d{1,14}$Phone number (E.164)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$Date (YYYY-MM-DD)
^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$IPv4 address
^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$UUID v4
^#(?:[0-9a-fA-F]{3}){1,2}$Hex color code
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$Strong password (min 8 chars, upper+lower+digit)

パターンをクリックしてテスターに読み込む

このツールについて

正規表現は、プログラミング、データ処理、テキスト解析、その他無数のアプリケーション全体で使用される強力なパターンマッチング言語です。正規表現の簡潔な構文と数多くの特殊文字により、習得は困難に見えるかもしれませんが、基本的な概念——アンカー、量指定子、文字クラス、グループ、先読み・後読み——を理解すれば、複雑なテキスト問題を優雅に解決できます。この正規表現チートシートは、最も重要なパターンと概念を1つの対話的なリファレンスにまとめています。

キュレーションされたパターンと概念をカテゴリー別に閲覧してください:アンカー(^、$、\b)、量指定子(*、+、?、{n,m})、文字クラス([...]、\d、\s)、グループとキャプチャ(...、?:...)、先読み・後読み(肯定と否定)。ライブパターンテスターを使用して、独自のテスト文字列と正規表現を入力し、リアルタイムでマッチをハイライト表示できます。任意のパターンをワンクリックでコピーして、コードや正規表現デバッガーに直接貼り付けます。

構文をすばやく復習する必要があるときは、このツールをブックマークしてください。メールアドレスの検証、ログファイルの解析、HTMLからのデータ抽出、乱雑な文字列のクリーンアップ、コード検索など、あらゆる場面で正規表現は不可欠です。このツールのパターンはほとんどの言語(JavaScript、Python、Java、Goなど)と互換性がありますが、実装によって高度な機能が若干異なる場合があります。

よくある質問

コード実装

import re

# Common regex patterns
email_pattern = r'^[w.-]+@[w.-]+.[a-zA-Z]{2,}$'
url_pattern = r'https?://[w-._~:/?#[]@!$&'()*+,;=%]+'
ipv4_pattern = r'^(?:(?:25[0-5]|2[0-4]d|[01]?dd?).){3}(?:25[0-5]|2[0-4]d|[01]?dd?)$'

# Test email
email = "user@example.com"
if re.match(email_pattern, email):
    print(f"{email} is valid")

# Find all matches
text = "Contact us at info@example.com or support@test.org"
emails = re.findall(r'[w.-]+@[w.-]+.[a-zA-Z]{2,}', text)
print("Found emails:", emails)

# Named groups
date_text = "Today is 2024-03-15"
match = re.search(r'(?P<year>d{4})-(?P<month>d{2})-(?P<day>d{2})', date_text)
if match:
    print(f"Year: {match.group('year')}, Month: {match.group('month')}")

# Substitution
result = re.sub(r's+', ' ', "hello   world   foo").strip()
print(result)  # "hello world foo"

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.