Character Classes
| Pattern | Matches |
. | Any character (except newline) |
\d | Digit [0-9] |
\D | Non-digit |
\w | Word character [a-zA-Z0-9_] |
\W | Non-word character |
\s | Whitespace |
\S | Non-whitespace |
[abc] | a, b, or c |
[^abc] | NOT a, b, or c |
[a-z] | Lowercase letter |
Quantifiers
| Pattern | Meaning |
* | 0 or more |
+ | 1 or more |
? | 0 or 1 |
{3} | Exactly 3 |
{2,5} | Between 2 and 5 |
{3,} | 3 or more |
*? | 0 or more (lazy) |
+? | 1 or more (lazy) |
Anchors & Boundaries
| Pattern | Matches |
^ | Start of string/line |
$ | End of string/line |
\b | Word boundary |
Groups & Lookaround
(abc) # Capture group
(?:abc) # Non-capturing group
(?<name>abc) # Named group
\1 # Backreference to group 1
a|b # a OR b
(?=abc) # Lookahead (followed by abc)
(?!abc) # Negative lookahead
(?<=abc) # Lookbehind (preceded by abc)
(?<!abc) # Negative lookbehind
Flags
| Flag | Meaning |
g | Global (all matches) |
i | Case insensitive |
m | Multiline (^ and $ per line) |
s | Dotall (. matches newline) |