A regular expression, or regex, is a compact way to describe a pattern in text. With one line you can find phone numbers, check an email format, clean up spaces or rename hundreds of lines. Regex looks cryptic at first, but a small set of building blocks covers most everyday needs.
Literal characters
The simplest regex is just text. The pattern cat matches the letters c, a, t in that order wherever they appear, including inside "category".
Character classes
.matches any single character except a line break.\dmatches a digit, and\Dmatches anything that is not a digit.\wmatches a letter, digit or underscore, and\Wthe opposite.\smatches whitespace such as spaces and tabs.[abc]matches one of the listed characters,[a-z]matches a range and[^abc]matches anything except those.
Quantifiers: how many times
*means zero or more,+means one or more and?means zero or one.{3}means exactly three,{2,5}means two to five and{3,}means three or more.
For example, \d{4} matches four digits in a row, such as a year.
Anchors and boundaries
^matches the start of the text (or line with the multiline flag) and$the end.\bmatches a word boundary.\bcat\bfinds the word "cat" but not "category".
Groups and alternatives
Parentheses group parts of a pattern and capture the matched text. A vertical bar means "or". The pattern (Mr|Ms|Mrs)\. (\w+) matches titles followed by a name, and captures the title and name separately so you can reuse them in a replacement.
Flags
gfinds all matches instead of stopping at the first.iignores upper and lower case.mmakes ^ and $ work on each line.
Three useful examples
- Extra spaces:
\s{2,}finds two or more whitespace characters in a row, which you can replace with a single space. - Simple date like 2026-11-08:
\d{4}-\d{2}-\d{2} - Hex color like #4f46e5:
#[0-9a-fA-F]{6}\b
Greedy versus lazy
Quantifiers are greedy by default: they match as much as possible. On the text <b>one</b> and <b>two</b>, the pattern <b>.*</b> matches the entire string. Adding a question mark, as in <b>.*?</b>, makes it lazy, so it matches each tag pair separately.
Common mistakes
- Forgetting to escape special characters. To match a literal dot, write
\., not.. - Trying to validate everything with regex. A perfect email or URL pattern is extremely complicated. A simple pattern plus a confirmation step is usually better.
- Patterns that are too broad.
.*is tempting but often matches more than you want. - Slow patterns. Nested quantifiers such as
(a+)+can cause catastrophic backtracking on certain inputs.
Test as you build
The best way to learn regex is to experiment. Paste sample text into our Regex Tester, build your pattern a piece at a time and watch the matches and capture groups highlight live. Note that regex syntax differs slightly between programming languages. Our tester uses the JavaScript flavor.
Frequently asked questions
Is regex the same in every programming language?
The core is similar, but details differ, such as lookbehind support, named groups and flags. Always test in the flavor your language uses.
How do I match a literal dot or bracket?
Escape it with a backslash, for example \. for a dot. Inside a character class, many symbols lose their special meaning.
Should I use regex to validate email addresses?
Use a simple check, such as an @ sign and a dot in the domain, then confirm the address by sending a message. Fully correct patterns are enormous and still cannot tell you whether the mailbox exists.