Regular expressions, usually called regex, look like random symbols to beginners. In reality they are a compact language for describing patterns in text. Once you understand a handful of building blocks, you can validate forms, search logs, clean data and rewrite text in seconds. This guide teaches those building blocks with practical examples.
What Is a Regular Expression?
A regular expression is a pattern that describes a set of strings. Instead of searching for the exact word "cat", you can search for "any three-letter word that starts with c and ends with t". Almost every programming language, text editor and database supports regex, with small differences between flavors.
The Core Building Blocks
Literal characters
Most characters match themselves. The pattern hello matches the text "hello".
Character classes
.matches any single character except a line break.\dmatches a digit,\wmatches a letter, digit or underscore, and\smatches whitespace.[abc]matches any one of a, b or c.[a-z]matches any lowercase letter.[^0-9]matches anything that is not a digit.
Quantifiers
*means zero or more,+means one or more, and?means zero or one.{3}means exactly three, and{2,5}means between two and five.
Anchors
^ matches the start of a line and $ matches the end. Anchors are essential for validation, because without them a pattern can match just a part of the text.
Groups and alternation
Parentheses group parts of a pattern and capture the matched text. The pipe symbol means "or", so cat|dog matches either word.
Practical Examples
- Find four-digit years:
\b\d{4}\b - Simple email shape check:
^[^\s@]+@[^\s@]+\.[^\s@]+$. This confirms the general shape only. The only true way to verify an email address is to send a message to it. - Remove extra spaces: search for
\s{2,}and replace with a single space. - Extract hashtags:
#\w+ - Match a date like 2026-11-19:
\d{4}-\d{2}-\d{2}
Flags You Should Know
g(global) finds all matches instead of stopping at the first.imakes matching case insensitive.mmakes^and$work on each line.slets the dot match line breaks as well.
Greedy vs. Lazy Matching
By default quantifiers are greedy, meaning they match as much text as possible. In the text <b>one</b> and <b>two</b>, the pattern <b>.*</b> matches the whole string. Adding a question mark, as in <b>.*?</b>, makes it lazy so that it matches each tag pair separately.
Common Mistakes
- Forgetting to escape special characters. To match a literal dot or parenthesis, put a backslash before it.
- Missing anchors. A phone number validator without
^and$will accept text that merely contains a phone number. - Overly complex patterns. If a pattern is hard to read, split the task into steps or add comments.
- Catastrophic backtracking. Nested quantifiers such as
(a+)+can make some inputs take extremely long. Keep patterns simple and test them with long strings. - Parsing HTML with regex. For anything beyond very simple extraction, use a proper parser.
The Best Way to Learn: Test Live
Regex is easiest to understand when you can see matches highlighted as you type. Our Regex Tester highlights every match, lists capture groups and runs entirely in your browser. Start with tiny patterns, add one piece at a time, and watch how the matches change.
Final Thoughts
You do not need to memorize everything. Learn the character classes, quantifiers, anchors and groups, keep a reference nearby, and practice on real text. Within a few sessions, regex goes from intimidating to indispensable.