Regex Cheat Sheet: Patterns Every Developer Should Know
A practical regex cheat sheet — character classes, quantifiers, anchors, groups and the 10 patterns developers use most, with examples you can test live.

Contents
- How a regex pattern is read
- Anchors: where the match must sit
- Character classes: what counts as one character
- Quantifiers: how many times
- Groups and alternation: structure inside a pattern
- 10 patterns developers actually use
- Flags: the switches that change everything
- Where regex should not be used
- Test a pattern in 4 steps
- 5 mistakes to avoid
- Regex in JavaScript, Python and PHP — what changes
- Practise: 5 patterns to try right now
- The short version
The short answer: a regular expression (regex) is a tiny pattern language for describing text — "a word, then @, then a domain" or "exactly 6 digits". You write the pattern once, and the regex engine finds every piece of text that matches it. Developers use regex for validating forms, searching logs, extracting data and renaming files.
If you have ever needed to check whether a string is a valid email, pull all the phone numbers out of a document, or find every date written as 2026-11-19, regex is the tool. The free Regex Tester lets you paste a pattern and test text side by side and see the matches highlight live.
How a regex pattern is read
A regex engine reads your pattern left to right and tries to match it against the text, one character at a time. Most characters match themselves — the pattern cat matches the letters c-a-t. The power comes from the special characters, called metacharacters, which mean something else: . means "any character", * means "zero or more of the previous thing", ^ means "start of the string".
So c.t matches cat, cut and c3t, but not cart. The dot is a wildcard for exactly one character. That single idea — literal characters plus a handful of wildcards — is the whole foundation. Everything below is just a bigger vocabulary built on it.
Anchors: where the match must sit
| Pattern | Matches | Example |
|---|---|---|
^abc | abc at the start of the string | ^Hello matches "Hello world", not "say Hello" |
abc$ | abc at the end of the string | com$ matches "techerics.com", not "comedian" |
\bword\b | word as a whole word | \bcat\b matches "the cat sat", not "concatenate" |
\B | Not at a word boundary | Used rarely, mostly inside larger patterns |
Anchors don't consume characters — they assert a position. That is why ^ and $ are so useful for validation: ^\d+$ means "the whole string, from start to finish, is digits only". Without the anchors, \d+ would happily match the digits inside "abc123def" and your "numbers only" check would pass a string that isn't a number at all.
Character classes: what counts as one character
| Pattern | Matches |
|---|---|
. | Any single character except a newline |
\d | Any digit (0–9) |
\D | Any non-digit |
\w | Word character: letter, digit or underscore |
\W | Any non-word character |
\s | Whitespace: space, tab, newline |
\S | Any non-whitespace character |
[abc] | One of a, b or c |
[^abc] | Any character except a, b or c |
[a-z] | Any lowercase letter |
[0-9] | Any digit (same as \d) |
[A-Za-z] | Any letter, upper or lower case |
Two habits make character classes easy. First, the caret ^ means opposite only when it is the first character inside square brackets — [^0-9] means "not a digit", while [0-9^] just adds a literal caret to the set. Second, inside brackets most metacharacters lose their special meaning: [.*+] matches a literal dot, star or plus, so you don't need to escape them there.
Quantifiers: how many times
| Pattern | Meaning | Example |
|---|---|---|
* | Zero or more | ab*c matches "ac", "abc", "abbc" |
+ | One or more | ab+c matches "abc", "abbc", not "ac" |
? | Zero or one (optional) | colou?r matches "color" and "colour" |
{n} | Exactly n times | \d{4} matches a 4-digit year |
{n,} | n or more times | \d{2,} matches two or more digits |
{n,m} | Between n and m times | \d{3,4} matches 3 or 4 digits |
*?, +? | Lazy versions — match as few as possible | <.+?> matches one tag, not the whole line (note: write it without spaces as <.+?>) |
The lazy quantifier deserves a callout because it fixes the most common regex surprise. The greedy pattern <. +> applied to <b>bold</b> and <i>italic</i> matches the entire string from the first < to the last > — one giant match. Add the ? to make it lazy, <. + ?> (written without spaces as <.+?>), and it matches each tag separately. Whenever a pattern swallows more than you expected, a lazy quantifier is usually the fix.
Groups and alternation: structure inside a pattern
| Pattern | Meaning |
|---|---|
(abc) | A capturing group — matches abc and remembers it |
(?:abc) | A non-capturing group — groups without remembering |
a|b | Alternation — matches a or b |
\1 | Backreference — the text matched by group 1 |
(?<name>...) | Named group (supported in JavaScript, Python, .NET) |
Groups do two jobs: they let a quantifier apply to several characters at once — (ab)+ matches "ab", "abab", "ababab" — and they capture pieces of the match for later use. In a find-and-replace, (\w+)@(\w+) lets you rearrange "user@domain" into "domain / user" by referring to $1 and $2. Non-capturing groups (?:...) are the same thing without the memory overhead, and they are the right default when you only need grouping, not capturing.
10 patterns developers actually use
These are copy-paste starting points. Test each one in the Regex Tester with both matching and non-matching text before you ship it — the examples show what each pattern does and doesn't catch.
- Email address (practical, not perfect) —
^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$— Matchesname@example.com. It rejects obvious junk like "name@" or "@example.com", but no regex validates every legal email — for real signup forms, pair it with a confirmation email. - Indian mobile number —
^[6-9]\d{9}$— Matches a 10-digit number starting with 6–9. Add(?:\+91[\s-]?)?at the front if you also want to accept the country code. - Date in YYYY-MM-DD form —
^\d{4}-\d{2}-\d{2}$— Matches2026-11-19. It checks the shape, not whether the date exists — 2026-13-45 passes this pattern, so validate the real date in code afterwards. - URL —
https?://[^\s/$.?#].[^\s]*— Matcheshttps://techerics.com/tools/calculator.html. Thehttps?covers both http and https, and[^\s]*stops the match at the first space. - Whole numbers and decimals —
^-?\d+(\.\d+)?$— Matches42,-7and3.14. The(\.\d+)?group makes the decimal part optional. - Username (letters, digits, underscore, 3–16 chars) —
^[A-Za-z0-9_]{3,16}$— Matchessaif_2026. The length limit{3,16}is doing real work here — without it, a one-character username would pass. - Hex colour code —
^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$— Matches#fffand#1a2b3c. The alternation inside the group handles both the short and long forms. - Whitespace-only lines / trimming —
^\s+$finds blank lines;^\s+|\s+$matches leading or trailing spaces for trimming. - Words, for counting or extracting —
\b\w+\bmatches every word.\b[A-Z][a-z]+\bmatches only capitalised words like "November" or "Delhi". - Duplicate words —
\b(\w+)\s+\1\bmatches "the the" or "had had" — the backreference\1means "the same word again".

Flags: the switches that change everything
A pattern on its own is only half the story. Flags change how the engine applies it:
| Flag | Effect | When you need it |
|---|---|---|
g (global) | Find all matches, not just the first | Counting words, extracting every email from a page |
i (ignore case) | cat also matches "Cat" and "CAT" | Searching user input where case doesn't matter |
m (multiline) | ^ and $ match at each line, not just the string ends | Validating or processing text line by line |
s (dotAll) | . also matches line breaks | Matching across multiple lines, like an HTML block |
u (unicode) | Full Unicode support | Emoji, accented letters, \p{L} letter classes |
The g flag is the one beginners forget most. Without it, a "find all emails" search stops after the first one and looks broken — the pattern is fine, the flag is missing. The i flag is the opposite trap: useful for search, dangerous for validation, because a case-insensitive username check can let "Admin" and "admin" collide.
Where regex should not be used
Regex is a text-matching tool, not a parser, and using it as one is how production bugs are born. Don't use regex to parse HTML — nested tags defeat it, and a proper parser handles malformed markup that a pattern never will. Don't use it as the only email validation — as the email pattern above shows, real addresses have edge cases no reasonable pattern covers; send a confirmation email instead. And don't use it for maths or date logic — a pattern can check that 2026-13-45 has the shape of a date, but only real date code knows that month 13 doesn't exist. The rule of thumb: regex checks shape, code checks meaning.
Test a pattern in 4 steps
- Start with the simplest version that could work. For an email, begin with
.+@.+\..+and tighten it only when you see a false match. - Feed it text that should match and text that should not. A pattern that accepts everything is as useless as one that accepts nothing — the non-matches are the real test.
- Check the edges. Empty string, one character too short, one too long, a trailing space. Most regex bugs live at the boundaries.
- Read the match, not just the yes/no. The tester highlights exactly what matched. If the highlight covers more or less than you intended, the pattern is wrong even when it says "match".
5 mistakes to avoid
- Forgetting the anchors.
\d+matches digits anywhere;^\d+$means the whole string is digits. Validation without^and$is barely validation at all. - Greedy quantifiers eating too much.
.*grabs everything it can. Use.*?(lazy) or a negated class like[^"]*when you want to stop at a boundary. - Not escaping special characters. To match a literal dot in a domain, write
\.— a bare.matches any character, sotecherics.comas a pattern also matches "techericsXcom". - Writing one giant pattern instead of two checks. "Valid email" is easier as a regex plus a length check plus a confirmation email than as a single 200-character pattern nobody can read.
- Catastrophic backtracking. Nested quantifiers like
(a+)+$on a long non-matching string can hang the engine. Keep patterns simple, and prefer a character class ([^"]*) over.*inside quotes.
Regex in JavaScript, Python and PHP — what changes
The patterns in this cheat sheet work almost identically in the three languages developers meet most, but the wrapping is different:
JavaScript: patterns live between slashes with flags after them — /^\d{10}$/.test("9876543210") returns true. The tester on this site uses the JavaScript engine, so anything that works there works in your browser code unchanged.
Python: patterns are strings passed to the re module — re.search(r"^\d{10}$", "9876543210"). The r prefix marks a raw string, so \d stays as backslash-d instead of Python trying to read it as an escape. Python also supports named groups as (?P<name>...) instead of JavaScript's (?<name>...).
PHP: patterns sit inside delimiters with preg_match — preg_match('/^\d{10}$/', "9876543210"). PHP's engine is PCRE, which is very close to JavaScript for everyday patterns but adds features like possessive quantifiers (a++) and \A / \Z anchors that JavaScript doesn't have.
The practical rule: write and debug the pattern in the tester first, then translate only the wrapping — slashes and flags in JavaScript, r"..." in Python, preg_match in PHP. The pattern itself almost never needs to change.
Practise: 5 patterns to try right now
The fastest way to learn regex is to break it on purpose. Open the tester and try these, one at a time:
- Find every year in a paragraph:
\b(19|20)\d{2}\b— then change it to match only years from 2000 onwards. - Extract prices like ₹1,299 or $49.99:
[₹$]\d[\d,]*(\.\d{2})?— watch what happens when a price has no decimal part. - Catch double spaces in a document: two spaces with
{2,}— then replace them with a single space using the replace box. - Find words ending in "ing":
\b\w+ing\b— then limit it to words of at least 6 letters. - Split a full name into first and last:
^(\w+)\s+(\w+)$— then try a three-part name and see why it fails.
Each exercise teaches one idea — boundaries, alternation, quantifiers, groups — and each failure teaches more than a success. When a pattern matches nothing, remove half of it and see what comes back; the part that broke is usually the last thing you added.
The short version
Regex is a small language for describing text: literal characters match themselves, metacharacters like ., \d and \w stand for classes of characters, quantifiers say how many times, anchors say where, and groups say what to remember. Start with the simplest pattern that could work, test it against text that should and shouldn't match, anchor your validations with ^ and $, and reach for a lazy quantifier whenever a greedy one swallows too much. The ten patterns above cover most everyday jobs — and the tester will show you exactly what each one matches before you trust it in production.
Frequently asked questions
What is regex?
Regex (regular expression) is a pattern language for describing and matching text. A pattern like ^\d{10}$ means "exactly 10 digits", and a regex engine finds or validates text that fits the pattern. Developers use it for form validation, searching logs, extracting data and renaming files.
How do I test a regex pattern?
Paste the pattern and some sample text into a regex tester such as the TechErics Regex Tester. It highlights every match live, so you can see exactly what your pattern catches — including whether it matches too much or too little — before you use it in code.
What does .* mean in regex?
The dot . matches any single character (except a newline) and the star * means "zero or more times", so .* matches any sequence of characters of any length. It is greedy by default, so .* between two quotes can swallow several quoted strings at once — use .*? (lazy) or [^"]* to stop at the first closing quote.
How do I validate an email address with regex?
A practical pattern is ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$. It catches obvious mistakes like a missing @ or domain, but no regex covers every legal email address, so real signup forms should also send a confirmation email.
What is the difference between + and * in regex?
The plus + means "one or more" of the previous element, while the star * means "zero or more". So ab+c requires at least one b ("abc" works, "ac" doesn't), while ab*c allows zero b's ("ac" works too).
What are regex groups?
Parentheses group part of a pattern so a quantifier can apply to several characters at once, and they capture the matched text for later use — (ab)+ matches repeated "ab", and in a replacement $1 refers to the first group. Non-capturing groups (?:...) group without capturing and are the better default when you don't need the captured text.
How do I match exactly 10 digits in regex?
Use ^\d{10}$ — the anchors ^ and $ force the whole string to be digits, and {10} requires exactly ten of them. Without the anchors, \d{10} would also match the first ten digits inside a longer string.
Why is my regex matching too much text?
Almost always because a quantifier is greedy: .* and .+ grab as much as possible. Make the quantifier lazy with a ? (.*?), or replace it with a negated character class that stops at the boundary you want, such as [^"]* to stop at the next quote mark.


