Regex Cheat Sheet for Beginners
Developer · October 3, 2026 · 2 min read
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.
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.(?:...)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.
Examples
^\d{4}-\d{2}-\d{2}$matches a date like 2026-10-16.#[0-9a-fA-F]{6}\bmatches a hex color like #6366f1.\b[\w.+-]+@[\w-]+\.[\w.]+\bfinds simple email addresses. (Perfect email validation with regex isn't practical, so use this only to find them.)
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.