Pola Dasar
Metakarakter
.Karakter apa pun (kecuali newline)
^Awal string / baris
$Akhir string / baris
*0 atau lebih dari sebelumnya
+1 atau lebih dari sebelumnya
?0 atau 1 dari sebelumnya (opsional)
\Escape metakarakter
Pencocokan Literal
hello # matches "hello" exactly a.c # matches "abc", "a1c", "a-c", etc. \.txt # matches literal ".txt"
Character Class
Ekspresi Bracket
[abc]Cocok dengan a, b, atau c
[^abc]Cocok dengan apa pun kecuali a, b, c
[a-z]Huruf kecil
[A-Z]Huruf besar
[0-9]Digit
[a-zA-Z0-9]Alfanumerik
Class Singkatan
\dDigit [0-9]
\DNon-digit [^0-9]
\wKarakter kata [a-zA-Z0-9_]
\WKarakter non-kata
\sWhitespace [ \t\n\r\f]
\SNon-whitespace
Quantifier
Quantifier Greedy
*0 atau lebih (greedy)
+1 atau lebih (greedy)
?0 atau 1 (greedy)
{n}Tepat n kali
{n,}n kali atau lebih
{n,m}Antara n dan m kali
Quantifier Lazy
*?0 atau lebih (lazy / non-greedy)
+?1 atau lebih (lazy)
??0 atau 1 (lazy)
{n,m}?Antara n dan m (lazy)

Quantifier lazy mencocokkan sesedikit mungkin karakter

Greedy vs Lazy
<.+> # greedy: "bold" <.+?> # lazy: ""
Anchor
^Awal string (atau baris dengan flag m)
$Akhir string (atau baris dengan flag m)
\bBatas kata
\BBukan batas kata
\AAwal string (tidak terpengaruh m)
\ZAkhir string (tidak terpengaruh m)
Contoh Anchor
^Hello # starts with "Hello" world$ # ends with "world" \bword\b # "word" as whole word \Bword\B # "word" inside another word
Grup & Alternasi
Capturing Group
(abc) # capture group: match "abc" (a|b|c) # alternation: a or b or c (cat|dog) # match "cat" or "dog" (\d{3})-(\d{4}) # groups: "123-4567"
Jenis Grup
(pattern)Capturing group
(?:pattern)Non-capturing group
(?P<name>pat)Named group (Python)
(?<name>pat)Named group (JS, .NET)
\1 \2Backreference ke grup 1, 2
a|bAlternasi: a atau b
Lookahead & Lookbehind
(?=pattern)Positive lookahead
(?!pattern)Negative lookahead
(?<=pattern)Positive lookbehind
(?<!pattern)Negative lookbehind
Contoh Lookaround
\d+(?= USD) # digits followed by " USD" \d+(?! USD) # digits NOT followed by " USD" (?<=\$)\d+ # digits preceded by "$" (?# digits NOT preceded by "$"

Lookaround mencocokkan posisi tanpa mengonsumsi karakter

Pola Umum
\d{1,3}(\.\d{1,3}){3}Alamat IPv4 (dasar)
[\w.+-]+@[\w-]+\.[\w.]+Email (dasar)
https?://[\w./\-?&#=]+URL (dasar)
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}Nomor telepon AS
\d{4}-\d{2}-\d{2}Tanggal (YYYY-MM-DD)
#?[0-9a-fA-F]{6}Kode warna hex

Ini adalah pola yang disederhanakan; penggunaan produksi mungkin perlu validasi lebih ketat

Flag
gGlobal: temukan semua kecocokan, bukan hanya yang pertama
iPencocokan case-insensitive
mMultiline: ^ / $ cocok batas baris
sDotall: . juga cocok newline
xVerbose: abaikan whitespace, izinkan komentar
uUnicode: dukungan Unicode penuh
Penggunaan Flag per Bahasa
/pattern/gi # JavaScript re.compile(r"pat", re.I | re.M) # Python grep -iE "pattern" # grep (extended)