Regular Expression Cheatsheet
Quick reference for common regex patterns
A Character Classes
| Character | Meaning | Example |
|---|---|---|
. |
Any character (except newline) | a.c matches "abc", "a1c" |
\d |
Digit [0-9] | \\d with quantifier matches 3 digits like "123" |
\D |
Non-digit [^0-9] | \D+ matches "abc" |
\w |
Word character [a-zA-Z0-9_] | \w+ matches "hello123" |
\W |
Non-word character | \W+ matches "!@#" |
\s |
Whitespace | \s+ matches " " |
\S |
Non-whitespace | \S+ matches "abc" |
[abc] |
Character set (a or b or c) | [abc] matches "a", "b", "c" |
[^abc]
|
Negated set (not abc) | [^abc] matches "d", "e", "f" |
[a-z] |
Range (lowercase letters) | [a-z]+ matches "hello" |
B Anchors (Position Matching)
| Character | Meaning | Example |
|---|---|---|
^ |
Start of string | ^hello matches "hello" at start |
$ |
End of string | world$ matches "world" at end |
\b |
Word boundary | \btest\b matches word "test" |
\B |
Non-word boundary | \Btest\B matches "test" in "atestb" |
C Quantifiers
| Quantifier | Meaning | Example |
|---|---|---|
* |
0 or more times | \d* matches "", "1", "123" |
+ |
1 or more times | \d+ matches "1", "123" |
? |
0 or 1 time | \d? matches "", "1" |
n |
Exactly n times | Digit with brackets n means exactly n times, like \\d3 matches 3 digits |
n, |
At least n times | Digit with brackets n, means at least n times, like \\d2, matches 2 or more digits |
n,m |
n to m times | Digit with brackets n,m means n to m times, like \\d2,4 matches 2 to 4 digits |
π‘ Greedy vs Lazy
By default, quantifiers are greedy (match as much as possible). Add
? to make them lazy (match as little as possible).
- β’
*?,+?,??,n,m?are lazy versions - β’
.*greedily matches as much as possible - β’
.*?lazily matches as little as possible
D Groups and References
| Syntax | Meaning | Example |
|---|---|---|
(abc) |
Capturing group | Parentheses capture matched content for later use |
(?:abc)
|
Non-capturing group | Parentheses only for grouping, don't capture content |
\1 |
Backreference to group 1 | (\w)\1 matches "aa", "bb" |
| |
OR operator | a|b matches "a" or "b" |
E Lookarounds
| Syntax | Meaning | Example |
|---|---|---|
(?=abc)
|
Positive lookahead (followed by abc) | \d(?=px) matches "1" in "1px" |
(?!abc)
|
Negative lookahead (not followed by abc) | \d(?!px) matches "1" in "1ab" |
(pos-lookbehind-abc)
|
Positive lookbehind (preceded by abc) | (pos-lookbehind-$)\d+ matches "100" in "$100" |
(neg-lookbehind-abc) |
Negative lookbehind (not preceded by abc) | (neg-lookbehind-$)\d+ doesn't match "$100" |
Want to Learn More?
Start from the basics and systematically master regular expressions, the powerful tool for text processing.
Start Learning Tutorial β