Regular Expressions (Regex)
Level 5 · IDs, dates and patterns
A short pattern that finds text by its shape. How \d, {3} and friends build a regex, how to test one, and when to stop and use code instead.
Course contentsLesson 11 of 11
Level 1 · Files and formats
Level 2 · Data formats
Level 3 · Config and docs files
Level 4 · Characters and escapes
Level 5 · IDs, dates and patterns
After this lesson you can
- Read a short regex piece by piece
- Find all matches in a line of text
- Know when a regex is the wrong tool
13 mincovers 3 glossary entries
Worth reading first: UUIDs, Unix Time and ISO 8601
How a short pattern finds text by its shape
A regular expression, or regex, is a short pattern that finds text by its shape, not its exact words. In the pattern \d{3}-\d{4}, \d means one digit and {3} means three of it. So it finds 555-0142 in a line of text and skips 12-30. Most languages and code editors support regex.
A regex works on a string, which is a piece of text inside a program. The pattern is read from left to right, one piece at a time. A plain letter matches that letter. A sign with a backslash stands for a kind of character.
A few signs do most of the work. \d is a digit, \w is a letter, digit or underscore, and \s is a space. A dot is any character. Square brackets list choices, so [aeiou] is any one vowel.
Other signs say how many. A star means zero or more, a plus means one or more, and a question mark means maybe one. Braces give an exact count, like {3}. A ^ ties the pattern to the start, and a $ ties it to the end.
Programs use regex to find, check and replace text. You can pull every phone number out of a page, check that a code has the right shape, or rename 200 files in one go. Most code editors also accept a regex in their search box.
Test a regex on real samples before you trust it. Try lines that should match and lines that should not. A pattern that looks right often matches too much, like a dot that also eats the spaces and the rest of the line.
The common mistake is one huge regex nobody can read. Past one line, split it into parts or write plain code instead. A regex is also the wrong tool for nested formats like JSON or HTML. Use a proper reader for those.
import re
print(re.findall(r"\d{3}-\d{4}", "Call 555-0142 or 555-0199"))
# ['555-0142', '555-0199']The regex signs you will meet first
| Sign | Means | Example match |
|---|---|---|
| \d | One digit | 7 |
| \w | One letter, digit or underscore | a |
| . | Any one character | % |
| {3} | Exactly three of the one before | 555 |
| + | One or more of the one before | aaa |
| ^ and $ | The start and the end of the text | (no character) |
Worked example
Lucy pulls order codes from emails
Lucy has 600 emails, and each one names an order code like SHOP-4821. She writes the pattern SHOP-\d{4} and runs it over all of them.
It finds 598 codes. The 2 it misses were typed as shop-4821 in small letters, so she adds a flag that ignores case.
The words for this lesson3
Check yourself
What does the regex \d{3} match?
Finished this lesson?
Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.