Basic Patterns
Metacharacters
.Any character (except newline)
^Start of string / line
$End of string / line
*0 or more of previous
+1 or more of previous
?0 or 1 of previous (optional)
\Escape metacharacter
Literal Matching
hello # matches "hello" exactly a.c # matches "abc", "a1c", "a-c", etc. \.txt # matches literal ".txt"
Character Classes
Bracket Expressions
[abc]Match a, b, or c
[^abc]Match anything except a, b, c
[a-z]Lowercase letter
[A-Z]Uppercase letter
[0-9]Digit
[a-zA-Z0-9]Alphanumeric
Shorthand Classes
\dDigit [0-9]
\DNon-digit [^0-9]
\wWord char [a-zA-Z0-9_]
\WNon-word char
\sWhitespace [ \t\n\r\f]
\SNon-whitespace
Quantifiers
Greedy Quantifiers
*0 or more (greedy)
+1 or more (greedy)
?0 or 1 (greedy)
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times
Lazy Quantifiers
*?0 or more (lazy / non-greedy)
+?1 or more (lazy)
??0 or 1 (lazy)
{n,m}?Between n and m (lazy)

Lazy quantifiers match as few characters as possible

Greedy vs Lazy
<.+> # greedy: "bold" <.+?> # lazy: ""
Anchors
^Start of string (or line with m flag)
$End of string (or line with m flag)
\bWord boundary
\BNon-word boundary
\AStart of string (not affected by m)
\ZEnd of string (not affected by m)
Anchor Examples
^Hello # starts with "Hello" world$ # ends with "world" \bword\b # "word" as whole word \Bword\B # "word" inside another word
Groups & Alternation
Capturing Groups
(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"
Group Types
(pattern)Capturing group
(?:pattern)Non-capturing group
(?P<name>pat)Named group (Python)
(?<name>pat)Named group (JS, .NET)
\1 \2Backreference to group 1, 2
a|bAlternation: a or b
Lookahead & Lookbehind
(?=pattern)Positive lookahead
(?!pattern)Negative lookahead
(?<=pattern)Positive lookbehind
(?<!pattern)Negative lookbehind
Lookaround Examples
\d+(?= USD) # digits followed by " USD" \d+(?! USD) # digits NOT followed by " USD" (?<=\$)\d+ # digits preceded by "$" (?# digits NOT preceded by "$"

Lookarounds match a position without consuming characters

Common Patterns
\d{1,3}(\.\d{1,3}){3}IPv4 address (basic)
[\w.+-]+@[\w-]+\.[\w.]+Email (basic)
https?://[\w./\-?&#=]+URL (basic)
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}US phone number
\d{4}-\d{2}-\d{2}Date (YYYY-MM-DD)
#?[0-9a-fA-F]{6}Hex color code

These are simplified patterns; production use may need stricter validation

Flags
gGlobal: find all matches, not just first
iCase-insensitive matching
mMultiline: ^ / $ match line boundaries
sDotall: . matches newline too
xVerbose: ignore whitespace, allow comments
uUnicode: full Unicode support
Flag Usage by Language
/pattern/gi # JavaScript re.compile(r"pat", re.I | re.M) # Python grep -iE "pattern" # grep (extended)