Regex Cheat Sheet: Syntax You Actually Use
Regular expressions have a huge spec, but 90% of real-world usage comes from about 20 tokens. Here they are.
Character classes
| Token | Matches |
. | Any character except newline |
\d | Any digit (0-9) |
\D | Any non-digit |
\w | Word character (letters, digits, underscore) |
\s | Whitespace (space, tab, newline) |
[abc] | Any of a, b, or c |
[^abc] | Anything except a, b, or c |
[a-z] | Any lowercase letter, a through z |
Quantifiers
| Token | Meaning |
* | Zero or more |
+ | One or more |
? | Zero or one (optional) |
{n} | Exactly n times |
{n,m} | Between n and m times |
Anchors and boundaries
| Token | Meaning |
^ | Start of string (or line, with the m flag) |
$ | End of string (or line, with the m flag) |
\b | Word boundary |
Groups and lookarounds
| Token | Meaning |
(abc) | Capture group |
(?:abc) | Non-capturing group |
a|b | Match a or b |
(?=abc) | Positive lookahead — must be followed by "abc" |
(?!abc) | Negative lookahead — must not be followed by "abc" |
Example: matching an email-like string
\b[\w.+-]+@[\w-]+\.[a-zA-Z]{2,}\b
This is fine for quick filtering, but note that a fully RFC-compliant email regex is notoriously complex — for real validation, send a confirmation email instead of relying purely on regex.
Try it yourself
Paste any pattern into the Regex Tester to see live matches highlighted against your own sample text.