Meta Characters
- .Any character (except newline)
- \dDigit [0-9]
- \DNon-digit
- \wWord char [a-zA-Z0-9_]
- \WNon-word char
- \sWhitespace
- \SNon-whitespace
Anchors
- ^Start of string/line
- $End of string/line
- \bWord boundary
- \BNon-word boundary
- \AStart of string
- \ZEnd of string
Quantifiers
- *0 or more
- +1 or more
- ?0 or 1 (optional)
- {n}Exactly n times
- {n,}n or more times
- {n,m}Between n and m
- *? +? ??Non-greedy
Character Classes
- [abc]a, b, or c
- [^abc]Not a, b, or c
- [a-z]Range a to z
- [A-Z]Range A to Z
- [0-9]Range 0 to 9
- [a-zA-Z0-9]Alphanumeric
Groups & Capturing
# Capturing group
(pattern) # Captures match as $1, $2, etc.
# Non-capturing group
(?:pattern) # Groups but doesn't capture
# Named group
(?<name>pattern) # Capture as named group
# Alternation
(cat|dog) # Match "cat" or "dog"
# Backreference
\1 # Reference to first capture group
Common Patterns
# Email (simplified)
[\w.-]+@[\w.-]+\.\w+
# Phone (US format)
\d{3}[-.]?\d{3}[-.]?\d{4}
# URL
https?://[\w.-]+(?:/[\w.-]*)*
# IP Address
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
# Date (YYYY-MM-DD)
\d{4}-\d{2}-\d{2}
# Hex Color
#[0-9A-Fa-f]{6}\b
Flags/Modifiers
- iCase insensitive
- gGlobal (all matches)
- mMultiline (^ $ per line)
- sDotall (. matches newline)
- xExtended (allow whitespace)
Escape Sequences
- \\Literal backslash
- \.Literal dot
- \*Literal asterisk
- \nNewline
- \tTab
- \rCarriage return
Lookahead & Lookbehind
# Positive lookahead - match if followed by
foo(?=bar) # "foo" only if followed by "bar"
# Negative lookahead - match if NOT followed by
foo(?!bar) # "foo" only if NOT followed by "bar"
# Positive lookbehind - match if preceded by
(?<=foo)bar # "bar" only if preceded by "foo"
# Negative lookbehind - match if NOT preceded by
(?<!foo)bar # "bar" only if NOT preceded by "foo"