Regex Cheat Sheet: 4 Proven Patterns + The Complete Basics

Most regex guides hand you a wall of symbols and wish you luck. This regex cheat sheet with examples takes the opposite approach: it is pattern-first. Every pattern below is shown with strings it matches, strings it misses, and an honest note about where it breaks down. Before publishing, I ran each pattern against matching and non-matching sample strings using Python’s re module, so the results you see are verified, not guessed. If you want to experiment as you read, open our regex tester in another tab — it runs entirely in your browser, so nothing you paste is uploaded anywhere.

Regular expressions (regex) are a mini-language for describing text patterns. You use them to find, validate, and extract strings: email addresses in a contact list, dates in a log file, prices in a spreadsheet export. They work in nearly every programming language, in code editors like VS Code, and in command-line tools like grep. The dozen building blocks below cover most patterns you will ever meet. Keep that definition handy: this regex cheat sheet is a phrasebook for that mini-language.

Table of Contents

Character Shorthands: d, w, s and the Dot

Shorthands are the atoms of regex. Three of them do most of the work: Atoms first — every regex cheat sheet starts here because everything else is built from these.

  • d matches any single digit (0–9). In the string abc123, d finds 1; in abc it finds nothing at all.
  • w matches any “word” character: letters, digits, and the underscore — equivalent to [A-Za-z0-9_]. Against user_1!, the pattern w+ matches user_1 and stops at the exclamation mark. Against a lone !, w matches nothing.
  • s matches whitespace: spaces, tabs, and line breaks. Splitting too manytspaces on s+ gives you too, many, spaces — one pattern handles messy, uneven spacing.

The dot . matches any single character except a newline. That makes it powerful and dangerous in equal measure — more on that in the character-classes section, where forgetting to escape it is the classic beginner bug. Powerful and dangerous: the two adjectives that describe half of this regex cheat sheet.

Each shorthand has an uppercase opposite — D, W, S — so S+ returns every word in a sentence, skipping the gaps.

Anchors: ^ and $ Pin the Match

By default a pattern matches anywhere inside the text; anchors pin it to a position — ^ to the start, $ to the end. Position matters: anchors are the section of the regex cheat sheet beginners underuse most.

The pattern ^d+ matches 42 in 42 is the answer but finds nothing in the answer is 42, because the digits are not at the start. Flip it around: d+$ matches 42 in the answer is 42 and nothing in 42 is the answer. Read that example twice — it is the canonical regex cheat sheet lesson on anchoring.

Anchors are what turn a “find” pattern into a “validate” pattern. d{4}-d{2}-d{2} will happily match a date buried inside a longer string, but ^d{4}-d{2}-d{2}$ only matches when the entire input is a date. Whenever you validate form input, anchor both ends.

Quantifiers: How Many Times?

Quantifiers say how many times the thing before them may repeat: How many times: the question this part of the regex cheat sheet answers.

  • * — zero or more. go*gle matches ggle, google, and goooogle.
  • + — one or more. go+gle matches google but not ggle, because at least one o is required.
  • ? — zero or one (optional). colou?r matches both color and colour.
  • {n} — exactly n times. d{3} matches 555 in call 555 now but finds nothing in call 55 now.
  • {n,m} — between n and m times. d{2,4} matches 2026 in year 2026!.

Quantifiers are greedy by default: against <b>hi</b>, <.+> matches the whole <b>hi</b>, stretching from the first < to the last >. Add a ? to make it lazy — <.+?> matches just <b>. Same with quotes: ".+" on say "hi" then "bye" grabs "hi" then "bye", while ".+?" grabs only "hi". When a pattern matches too much, laziness is usually the fix. Greedy by default, lazy with a question mark — the regex cheat sheet rule you will use weekly.

Character Classes: Build Your Own Sets

Square brackets define your own set of allowed characters: [aeiou] matches any single vowel (searching regex finds e and e), and ranges like [a-z] match any lowercase letter — [a-z]+ on ABCdef matches def. A leading ^ negates the set: [^0-9] matches anything but digits, so searching call 555-1234 returns call and -. Build-your-own sets: the most creative corner of any regex cheat sheet.

Inside a character class, most special characters lose their powers — but outside one, the dot is special and must be escaped to mean a literal period. The pattern d+.d+ matches 3.14 in pi is 3.14 and correctly rejects 3x14. Forget the backslash and d+.d+ matches 3x14 too, because the unescaped dot happily matches the x. This is one of the most common regex bugs in the wild: if your “decimal number” pattern is matching things like 3x14, check your escaped dots first.

Groups and Alternation

Parentheses group parts of a pattern and capture what they match for later use. (d{4})-(d{2})-(d{2}) on Date: 2026-09-26 captures 2026, 09, 26 — referenceable as $1, $2, $3 in replacements, handy for reformatting dates. Group it, capture it, reuse it — the regex cheat sheet pattern for structured text.

For grouping without capturing, use (?:...): (?:Mr|Ms).sw+ matches Ms. Ada without storing anything. The pipe | means “or” — cat|dog finds both words in a cat and a dog. Combined: .(jpg|png|webp)$ matches image filenames by extension.

Regex Email Pattern: The Practical Version

Email validation is where regex reputations go to die: a fully RFC-correct pattern is thousands of characters long and unreadable — nobody should paste that into a signup form. What you want is the pragmatic version that catches typos and passes real addresses: Email validation is where every regex cheat sheet earns its honesty badge.

[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,}

In plain English: one or more allowed characters, an @, a domain name, a literal dot, and a top-level domain of at least two letters. I tested it against a battery of samples. It fully matches name@example.com and user.name+tag@mail.co.uk (plus-addressing and multi-part domains included). It correctly rejects not-an-email, user@localhost (no top-level domain), and a@@b.com. Tested against matches and misses: the standard this regex cheat sheet holds itself to.

Treat this pattern honestly: a 99% filter, not a proof. It may accept a few technically invalid addresses; its job is catching obvious mistakes before you send a confirmation email — the only true validation. Prefer no regex at all? Our email extractor pulls every address out of a block of text for you. A 99% filter, not a proof — the honest framing this regex cheat sheet insists on.

Regex Cheat Sheet in Action: URL, Date, Phone, and IP

Four copy-paste patterns for jobs that come up constantly, each verified against matching and non-matching inputs. Four patterns, each verified: the working core of this regex cheat sheet.

URL

https?://[w.-]+.[A-Za-z]{2,}(/[^s]*)? fully matches https://getanythingpro.online/tools/regex-tester/ and finds https://example.com/page?q=1 inside a sentence. It ignores not a url and skips ftp:// links — extend https? for other schemes. Good for finding links in text; not a substitute for a real URL parser when security matters.

Date (YYYY-MM-DD)

d{4}-d{2}-d{2} matches 2026-09-26 and rejects day-first 26-09-2026. Honest caveat, verified in testing: it also matches 2026-99-99 — the pattern checks format, not the calendar. Validate the numbers in code after the regex does the shape-checking.

Phone digits

+?d[ds().-]{7,}d matches +1 (555) 123-4567 and +92 300 1234567, and finds nothing in call me maybe. On My number is +92 300 1234567, call after 5pm it extracts exactly +92 300 1234567. Treat it as a finder — phone formats vary by country — then normalize the digits in code.

IP address (ish)

bd{1,3}.d{1,3}.d{1,3}.d{1,3}b finds 192.168.0.1 in server 192.168.0.1 down and ignores version 3.14. The “ish” matters: it also matches 999.999.999.999, which is not a real IP (each octet must be 0–255). A stricter octet-validating pattern exists but is far uglier — for log scanning, find candidates with this one and validate the numbers afterwards.

The theme: regex is excellent at finding candidates shaped like what you want; final validation of emails, dates, and IPs usually belongs in code. If you are pulling structured data out of messy text, our guide on what a JSON formatter does covers making extracted data readable and valid. Candidates, not verdicts: the mature lesson of this regex cheat sheet.

Catastrophic Backtracking: The One Trap to Avoid

One regex mistake does not just fail — it hangs. Catastrophic backtracking comes from nesting quantifiers, like (a+)+$ (“one or more groups of one or more a’s, at the end”). On 22 a’s followed by a b — which can never match — the engine tries every possible way of splitting the a’s into groups before giving up, and the combinations double with each extra character. One trap, clearly marked: no regex cheat sheet is complete without this warning.

Measured: (a+)+$ on 22 a’s plus a b took about 1.3 seconds to report no match in Python. A few more a’s and you wait minutes; a dozen more and you wait effectively forever. The fix is to simplify — plain a+$ fails on the same input instantly. Whenever nested +, *, or {n,m} feels slow, flatten it.

Try It Live: Regex Tester Online

Reading patterns is one thing; watching them match is how they stick. Paste any pattern from this cheat sheet into our regex tester online along with your own sample text, and you will see matches highlight as you type. It runs entirely in your browser — your test strings are never uploaded to our servers — so you can safely experiment with real data. Paste, watch, learn: the interactive way to use this regex cheat sheet.

A practical workflow: extract candidates with a pattern in the tester, clean up the results with our text cleaner (strip blank lines, fix spacing, remove duplicates), and if you need to reference the matching lines later, add line numbers to your text before sharing it. For the theory behind any pattern you do not recognize, the MDN guide to regular expressions is the most reliable reference on the web.

Frequently Asked Questions

What is the simplest regex pattern to learn first?

Start with d+ — “one or more digits.” It teaches you the two core ideas at once: a shorthand (d = digit) and a quantifier (+ = one or more). From there, w+ for words and s+ for whitespace complete the starter set, and anchors (^, $) teach you positioning. Start there, and the rest of the regex cheat sheet unfolds naturally.

Why doesn’t my email regex catch every invalid address?

Because email addresses are genuinely complicated — the full specification allows quoted strings, comments, and IP domains that no readable pattern covers. The pragmatic pattern in this guide catches typos and malformed input, which is all a form needs. Real validation is sending a confirmation email, not a longer regex.

What is the difference between * and + in regex?

* means “zero or more” — the thing before it may be absent entirely, so go*gle matches ggle. + means “one or more” — at least one is required, so go+gle does not match ggle. Use + when the characters must actually be there.

How can I test a regex pattern safely online?

Use a tester that runs in your browser, like our regex tester online — paste the pattern and sample text, and matches highlight live. Because everything runs locally in the page, your test data is never uploaded to our servers, which makes it safe for real-world strings.

Why is my regex so slow?

Almost always catastrophic backtracking: nested quantifiers like (a+)+ or (.*)* force the engine to try an exponentially growing number of combinations on non-matching input. Simplify the pattern — often a plain character class with a single quantifier does the same job instantly.