A pattern for text
You need to find every date in a document. Or check that an input looks like a phone number. Or pull the error codes out of ten thousand log lines. Searching for fixed text will not do it, because you do not know the exact text in advance. You know its shape.
A regular expression, usually shortened to regex, is a way of describing a shape of text. You write a pattern, and the computer finds every piece of text that fits it.
Regexes have a reputation for being unreadable:
(\d{4})-(\d{2})-(\d{2})But that line is built from a few small pieces, each with one simple meaning. Learn eight of them and you can read nearly any pattern.
The eight building blocks
Click through them in order. Each pattern is run against the sample text by your browser's real regex engine, and the matches are highlighted.
Pattern
catText
cat concat category dog
3 matches. Plain letters match themselves, wherever they appear, including inside longer words.
1. Literal text
Ordinary letters and digits match themselves. cat matches the three letters c, a, t in a row, wherever they occur, including in the middle of "concat".
2. The dot: any one character
. matches any single character. c.t matches "cat", "cot" and "c-t". It does not match "ct", because the dot must match something.
3. Square brackets: one of these
[aeiou] matches one character that is any of those listed. A dash makes a range: [a-z] is any small letter, [0-9] any digit. A ^ at the start reverses it: [^0-9] is any character that is not a digit.
4. Shorthands for common sets
| Shorthand | Matches | The opposite |
|---|---|---|
\d | A digit | \D |
\w | A letter, digit or underscore | \W |
\s | A space, tab or line break | \S |
5. Quantifiers: how many
A quantifier follows something and says how many times it may repeat.
| Quantifier | Meaning |
|---|---|
+ | One or more |
* | Zero or more |
? | Zero or one: optional |
{4} | Exactly four |
{2,5} | Between two and five |
\d matched each digit of "66" separately. \d+ matches "66" as one piece.
6. Anchors: where
^ means the start of the text and $ means the end. They match a position, not a character. ^Order only matches "Order" when it is the very first thing.
Anchors matter most when checking input. \d+ finds digits anywhere in "abc123xyz". ^\d+$ says the whole text must be digits.
7. Groups and alternatives
| means "or". Round brackets group things together, so (cat|dog)s? means: either "cat" or "dog", then an optional "s".
Brackets also capture. Whatever matched inside them is saved, so that you can use that piece separately afterwards.
8. The backslash: escape
Characters such as ., *, +, ?, (, [ and $ have special meanings. To match one literally, put a backslash before it. \. matches a real full stop.
Reading a full pattern
Select the last block, "All together", and read the pattern from left to right.
(\d{4})
Four digits, captured as group 1. The year.
-
A dash, matching itself.
(\d{2})
Two digits, captured as group 2. The month.
-
Another dash.
(\d{2})
Two digits, captured as group 3. The day.
It finds both dates and ignores "v1-2-3", which has dashes but the wrong number of digits. Nothing in it is new: blocks 1, 4, 5 and 7.
import re
text = "Released 2026-03-09, patched 2026-11-22"
for match in re.finditer(r"(\d{4})-(\d{2})-(\d{2})", text):
year, month, day = match.groups()
print(year, month, day)
# 2026 03 09
# 2026 11 22Three traps
Quantifiers are greedy. They match as much as they can.
Example
Greedy and lazy
The text is <b>one</b> and <b>two</b> and the pattern is <b>.*</b>.
You might expect two matches. You get one: the whole line, from the first <b> to the last </b>, because .* grabbed as much as possible.
Add a ? to make it lazy: <b>.*?</b> matches as little as possible, and now finds each pair.
Forgetting the anchors when validating. Without ^ and $, a pattern only has to match somewhere inside the input, so "abc123" passes a check for \d+.
Trying to do too much. Regexes describe flat patterns. They are the wrong tool for nested structures like HTML or JSON, and a "complete" pattern for an email address is famously enormous. Use a regex for the simple shape, and a real parser or a library for the rest.
Key takeaways
- A regular expression describes a shape of text, and finds everything that fits it.
- Literals match themselves. A dot is any character. Square brackets are one character from a set.
- \d, \w and \s are shorthands. Quantifiers such as +, *, ? and {n} say how many.
- ^ and $ anchor to the start and end. Brackets group and capture. A backslash escapes.
- Quantifiers are greedy by default, and validation needs both anchors.