# Regular Expressions (Regex)

> 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:** [JSON, YAML and File Formats](https://learn.benibix.com/learn/development-formats)  
**Time:** 13 min

**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

## 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.

```python
import re
print(re.findall(r"\d{3}-\d{4}", "Call 555-0142 or 555-0199"))
# ['555-0142', '555-0199']
```
Find every match of three digits, a dash and four digits in one line of text.

## 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) |

![The pattern backslash d braces 3, a dash, then backslash d braces 4, drawn as three boxes labelled three digits, a dash and four digits. Notes say backslash d means one digit and braces 3 means three of it. An arrow leads down to a line of text: Call 555-0142 or 555-0199 by 12-30. The two phone numbers are boxed and marked match. 12-30 is marked no match, since it has too few digits.](https://learn.benibix.com/diagrams/dev-regex.svg)

### 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.

**Words in this lesson:** [Regular Expression (Regex)](https://learn.benibix.com/term/development/regular-expression), [String](https://learn.benibix.com/term/development/string), [Input Validation](https://learn.benibix.com/term/development/input-validation)

**Question:** What does the regex \d{3} match?

**Answer:** Exactly three digits in a row. \d means one digit, and {3} means three of the thing before it. Together they match three digits in a row.

---

Source: https://learn.benibix.com/learn/development-formats/regular-expressions-regex
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
