Regex Cheat Sheet for Beginners
Developer · Published October 3, 2026 · Updated October 24, 2026 · 3 min read
Written by Akinrinmade Michael
A regular expression, or regex, is a pattern that describes text. You use it to find, check or replace pieces of a string. It looks like line noise at first, but a handful of building blocks do most of the work, and once you know them you can read most patterns you'll come across.
Matching characters
.matches any one character (except a line break).\dmatches a digit,\wa letter, digit or underscore,\sa space or tab. Capital versions (\D,\W,\S) mean the opposite.[abc]matches a, b or c.[a-z]matches any lowercase letter.[^abc]matches anything except a, b or c.\.matches a real dot. Put a backslash before any special character to match it literally.
Repeating
*means zero or more,+means one or more,?means zero or one.{3}means exactly 3,{2,4}means 2 to 4,{2,}means 2 or more.- Add
?after a quantifier (.*?) to make it lazy, so it matches as little as possible instead of as much as possible.
Position and choice
^matches the start and$the end of the text (or of each line with themflag).\bmatches a word boundary, so\bcat\bfinds "cat" but not "concatenate".a|bmatches a or b.
Groups
(...)groups part of the pattern and captures what it matched, available as$1,$2and so on.(?<year>\d{4})gives a group a name, so you can use$<year>in a replacement. Python writes it(?P<year>...).(?:...)groups without capturing.(?=...)and(?!...)look ahead without using up characters.
Flags
gfinds all matches, not just the first.iignores upper and lower case.mmakes^and$work on every line.slets.match line breaks.uturns on full Unicode support.
Building a pattern step by step
Don't try to write a pattern in one go. Start with the simplest thing that matches part of what you want, and add to it while you watch the matches. Say you want to find dates like 2026-10-16 in a document:
- Start with
\dto see that it highlights single digits. - Make it four digits:
\d{4}. Now it finds years, but also any four-digit number. - Add the rest:
\d{4}-\d{2}-\d{2}. Now it needs the dashes. - Add boundaries so it doesn't match inside a longer number:
\b\d{4}-\d{2}-\d{2}\b. - Add groups to pull the parts out:
\b(\d{4})-(\d{2})-(\d{2})\b. Replace with$3/$2/$1to turn the dates into day, month, year.
Examples
| Goal | Pattern |
|---|---|
| A date like 2026-10-16, whole string | ^\d{4}-\d{2}-\d{2}$ |
| A hex color like #6366f1 | #[0-9a-fA-F]{6}\b |
| Hashtags | #\w+ |
| Two or more spaces | \s{2,} |
| Words in capitals | \b[A-Z]{2,}\b |
| Simple email addresses | \b[\w.+-]+@[\w-]+\.[\w.]+\b |
Perfect email validation with regex isn't practical, since the real rules are huge, so use the last pattern only to find addresses and not to prove they exist.
Greedy or lazy, once more
On the text <b>one</b> and <b>two</b>, the pattern <b>.*</b> matches the whole line, because .* grabs as much as it can and stops at the last closing tag. Make it lazy, <b>.*?</b>, and you get two separate matches. If your match swallows more than you meant, a missing question mark is the usual reason.
Common mistakes
- Forgetting to escape a dot.
a.bmatches a, any character, then b. Usea\.bfor a literal dot. - Forgetting anchors.
\d{4}also matches the first four digits of a ten-digit number. - Putting a pattern in a code string without doubling the backslashes.
\doften has to be written\\d, and the String Escape tool does it for you. - Expecting the same syntax everywhere. Features like lookbehind and named groups differ between JavaScript, Python, Java and PHP.
When regex is the wrong tool
Regular expressions are poor at anything with nested structure. Don't use them to parse HTML or JSON, or to check that brackets are balanced. Use a real parser. They're also hard to read when they get long, so if a pattern needs a paragraph of explanation, consider breaking the job into two simpler steps.
Watch out for slow patterns
Patterns with nested repeats, such as (a+)+$, can take a very long time on certain text. This is called catastrophic backtracking. If a pattern feels slow, make it more specific.
Practice with the Regex Tester: it highlights every match and shows the capture groups as you type. Use the Text Diff tool to compare text before and after a replacement.