🔤 Character Classes

Pattern Meaning Example Match
. Any character except newline a, 1, @
\\w Word character ([a-zA-Z0-9_]) a, 9, _
\\W Non-word character ([^a-zA-Z0-9_]) @, space
\\d Digit ([0-9]) 09
\\D Non-digit ([^0-9]) a, -
\\s Whitespace (space, tab, newline) ' ', \\n
\\S Non-whitespace a, 1, @

📏 Quantifiers

Pattern Meaning Example
* 0 or more lo*l, lo, loo
+ 1 or more lo+lo, loo, not l
? 0 or 1 colou?rcolor, colour
{n} Exactly n a{3}aaa
{n,} n or more a{2,}aa, aaa, aaaa
{n,m} Between n and m a{2,4}aa, aaa, aaaa

🎯 Anchors

Pattern Meaning Example
^ Start of string ^Hello matches "Hello world"
$ End of string world$ matches "Hello world"
\\b Word boundary \\bword\\b matches "word" but not "sword"
\\B Not a word boundary \\Bend matches "bend", not "end"

🎨 Character Sets

Pattern Meaning Example
[abc] Matches a, b, or c [ch]at matches cat, hat
[^abc] Not a, b, or c [^a-z] excludes lowercase
[a-z] Range from a to z [0-9], [A-Z]
[a-zA-Z0-9_] Custom set All letters + numbers + underscore

🧩 Groups & Alternation

Pattern Meaning Example
(abc) Group abc together (ha)+ matches haha, hahaha
` ` OR operator
(?:...) Non-capturing group (?:ab)+ matches abab without capturing

🎁 Escaping Special Characters

Use \\ before a special character to match it literally:

Character Match This
\\. a literal .
\\* a literal *
\\\\ a literal backslash

📦 Common Shorthand Summary