Developer Reference
Regular Expressions Cheat Sheet
Regular expression syntax reference for character classes, anchors, quantifiers, and lookaround. Free, in-browser cheat sheet.
Regular expressions are a powerful tool for pattern matching and text processing across most programming languages. This reference covers the core syntax you need to construct patterns for validation, extraction, and transformation—from basic character classes to advanced lookahead and lookbehind assertions.
Character classes
| Token | Matches |
|---|---|
. | Any character except newline |
\d | Any digit (0-9) |
\D | Any non-digit |
\w | Word character (a-z, A-Z, 0-9, _) |
\W | Non-word character |
\s | Whitespace |
\S | Non-whitespace |
[abc] | a, b, or c |
[^abc] | Not a, b, or c |
[a-z] | Range a to z |
Anchors & quantifiers
| Token | Matches |
|---|---|
^ | Start of string / line |
$ | End of string / line |
\b | Word boundary |
* | 0 or more |
+ | 1 or more |
? | 0 or 1 |
{n} | Exactly n |
{n,} | n or more |
{n,m} | Between n and m |
*? | Lazy 0 or more |
Groups & lookaround
| Token | Matches |
|---|---|
(abc) | Capture group |
(?:abc) | Non-capturing group |
(?<name>abc) | Named group |
a|b | a or b (alternation) |
(?=abc) | Positive lookahead |
(?!abc) | Negative lookahead |
(?<=abc) | Positive lookbehind |
(?<!abc) | Negative lookbehind |
\1 | Backreference to group 1 |
How this reference is used
- Validating email addresses or phone numbers in a form
- Extracting data like dates or URLs from unstructured text
- Finding and replacing patterns in code or configuration files
- Testing string patterns during development or debugging
- Learning regex syntax for a new language or framework
Frequently asked questions
What is the difference between a character class and a character range?
A character class like [abc] matches any single character from the set. A range like [a-z] matches any character within that span. Both are shorthand for avoiding verbose alternation and make patterns more readable.
When should I use anchors like ^ and $ in my patterns?
Anchors tie your pattern to specific positions in the string. Use ^ and $ to match from the start and end respectively, preventing partial matches when you need the entire string to conform to your pattern.
How do lookahead and lookbehind assertions differ from capturing groups?
Lookahead and lookbehind assertions (like (?=...) and (?<=...)) check if a pattern exists without consuming characters or being captured. Capturing groups ((...)) extract matched text. Use assertions for conditional matching without side effects.