The most commonly needed regex patterns are: . (any character), * (zero or more), + (one or more), \d (any digit), \w (any word character), and ^/$ (start/end of line). Combine these to match emails, phone numbers, or any structured text pattern.
Regular expressions (regex) are patterns used to match, search, and manipulate text. They appear in programming, command-line tools, text editors, and database queries. Here is a practical cheat sheet and how to test patterns instantly.
Regex Basics
.— Matches any single character except newline.*— Matches 0 or more of the preceding element.+— Matches 1 or more of the preceding element.?— Matches 0 or 1 of the preceding element (makes it optional).^— Matches start of string.$— Matches end of string.[]— Character class.[abc]matches a, b or c.[^]— Negated class.[^abc]matches anything except a, b, c.{n,m}— Matches between n and m repetitions.|— Alternation (OR).cat|dogmatches cat or dog.()— Capturing group.\d— Digit (0-9).\Dis non-digit.\w— Word character (a-z, A-Z, 0-9, _).\Wis non-word.\s— Whitespace.\Sis non-whitespace.\b— Word boundary.
Common Regex Patterns
- Email:
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} - URL:
https?:\/\/[^\s/$.?#].[^\s]* - IP Address:
\b(?:\d{1,3}\.){3}\d{1,3}\b - Date (YYYY-MM-DD):
\d{4}-\d{2}-\d{2} - Phone (US):
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4} - Hex color:
#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})
How to Test Regex Online
Use Anonymiz Regex Tester — paste your pattern and test string, and see matches highlighted in real time. Supports JavaScript regex syntax. Free, runs in your browser.
Regex Flags
g— Global: find all matches, not just the first.i— Case-insensitive.m— Multiline:^and$match start/end of each line.s— Dotall:.matches newlines too.
Common Patterns You Will Actually Use
- Email address:
^[\w.+-]+@[\w-]+\.[a-zA-Z]{2,}$ - US phone number:
^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$ - URL:
^https?:\/\/[\w.-]+\.[a-zA-Z]{2,}(\/\S*)?$ - Hex color code:
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$ - Date (YYYY-MM-DD):
^\d{4}-\d{2}-\d{2}$
Character Classes and Quantifiers
[abc]— matches any one of a, b, or c[^abc]— matches any character except a, b, or c{n}— exactly n occurrences{n,m}— between n and m occurrences(?:...)— non-capturing group, groups without creating a backreference
Common Mistakes to Avoid
- Forgetting to escape special characters —
.,*,+,?, and others need a backslash (\.) when you want the literal character. - Greedy vs lazy matching —
.*matches as much as possible;.*?matches as little as possible. This matters when extracting content between delimiters. - Forgetting anchors — without
^and$, your pattern can match a substring anywhere in the text, not the whole string.


