Regex Cheat Sheet
Referensi regular expression interaktif dengan pengujian pola langsung dan contoh copy-on-click.
Penguji Langsung
Anchors
| Pola | Deskripsi | |
|---|---|---|
| ^ | Start of string / line | |
| $ | End of string / line | |
| \b | Word boundary | |
| \B | Non-word boundary | |
| \A | Start of string only | |
| \Z | End of string only |
Quantifiers
| Pola | Deskripsi | |
|---|---|---|
| * | 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
| Pola | Deskripsi | |
|---|---|---|
| . | Any character except newline | |
| \d | Digit [0-9] | |
| \D | Non-digit | |
| \w | Word char [a-zA-Z0-9_] | |
| \W | Non-word character | |
| \s | Whitespace | |
| \S | Non-whitespace | |
| [abc] | Character set — a, b, or c | |
| [^abc] | Negated set — not a, b, or c | |
| [a-z] | Character range |
Groups & Alternation
| Pola | Deskripsi | |
|---|---|---|
| (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|b | Alternation — a or b |
Flags
| Pola | Deskripsi | |
|---|---|---|
| g | Global — find all matches | |
| i | Case-insensitive | |
| m | Multiline — ^ and $ match line boundaries | |
| s | Dotall — . matches newline too | |
| u | Unicode mode | |
| y | Sticky — match at exact position |
Common Patterns
| Pola | Deskripsi | |
|---|---|---|
| ^[\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) |
Klik pola untuk memuatnya ke dalam penguji
Tentang alat ini
Ekspresi reguler adalah bahasa pencocokan pola yang kuat yang digunakan dalam pemrograman, pemrosesan data, analisis teks, dan aplikasi tak terhitung banyaknya. Mempelajari regex dapat terasa menakjubkan karena sintaksisnya yang ringkas dan banyak karakter khusus, tetapi memahami dasar-dasarnya——jangkar, kuantifier, kelas karakter, grup, dan lookaround——membuka kemampuan untuk menyelesaikan masalah teks kompleks dengan elegan. Lembar Curan Regex ini mengumpulkan pola dan konsep paling penting dalam referensi interaktif.
Cukup telusuri pola dan konsep yang dikurasi yang dikelompokkan menurut kategori: jangkar (^, $, \b), kuantifier (*, +, ?, {n,m}), kelas karakter ([...], \d, \s), grup dan penangkapan (..., ?:...), dan lookaround (lookahead dan lookbehind positif dan negatif). Penguji pola langsung memungkinkan Anda memasukkan string uji Anda sendiri dan regex untuk melihat kecocokan disorot secara real-time. Salin pola apa pun dengan satu klik dan tempel langsung ke kode atau alat debugging regex Anda.
Tandai alat ini kapan pun Anda memerlukan penyegaran cepat tentang sintaksis. Baik Anda memvalidasi alamat email, mengurai file log, mengekstrak data dari HTML, membersihkan string yang berantakan, atau menelusuri kode, regex sangat penting. Pola yang ditampilkan kompatibel dengan sebagian besar bahasa (JavaScript, Python, Java, Go, dan lainnya), meskipun beberapa fitur lanjutan sedikit berbeda di seluruh implementasi.
Pertanyaan yang Sering Diajukan
Implementasi Kode
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.