\d+ # One or more digits: "123", "5"\d{3} # Exactly 3 digits: "123", "456"\d{2,4} # 2 to 4 digits: "12", "123", "1234"colou?r # "color" or "colour"https? # "http" or "https"
Anchors
Pattern
Matches
^
Start of string/line
$
End of string/line
\b
Word boundary
\B
Not a word boundary
Examples:
^hello # "hello" at start: "hello world" ✓, "say hello" ✗world$ # "world" at end: "hello world" ✓, "world peace" ✗\bcat\b # "cat" as whole word: "cat" ✓, "category" ✗
Grouping and Alternation
Pattern
Meaning
(...)
Capturing group
(?:...)
Non-capturing group
`
`
Examples:
(cat|dog) # Matches "cat" OR "dog"gr(a|e)y # Matches "gray" OR "grey"(\d{3})-\d{4} # Matches "123-4567" (captures "123")