Regex Cheat Sheet for Beginners
Regular expressions look cryptic until you learn the handful of building blocks. Here's a beginner's cheat sheet.
Character classes
| Pattern | Matches |
|---|---|
. | any character (except newline) |
\d | a digit (0–9) |
\w | word character (a–z, A–Z, 0–9, _) |
\s | whitespace |
[abc] | a, b, or c |
[^abc] | anything except a, b, c |
[a-z] | any lowercase letter |
Quantifiers
| Pattern | Meaning |
|---|---|
* | 0 or more |
+ | 1 or more |
? | 0 or 1 (optional) |
{3} | exactly 3 |
{2,5} | between 2 and 5 |
Anchors
| Pattern | Meaning |
|---|---|
^ | start of string/line |
$ | end of string/line |
\b | word boundary |
Groups and alternation
(abc)— a capturing group(?:abc)— a non-capturing groupa|b— match a or b
Common flags
g— global (find all matches)i— case-insensitivem— multiline (^/$match each line)
A worked example
Match a simple time like 09:30:
^\d{2}:\d{2}$
Two digits, a colon, two digits, anchored to the whole string.
Practice
Test patterns live against your own text with the regex tester, and see common regex patterns for ready-made examples.
Got a config file to check?
Open the config toolkit →