What is Regex?
It’s a special language used to find, describe, and extract patterns in text. Instead of searching for a specific word, you search for a type of data.
The Anchors
The “Secret Guards” that tell you where to look.
| Symbol | Secret Job | Example | Matches |
|---|---|---|---|
^ | The Front Guard: Must be at the very start. | ^Hello | ”Hello there” |
$ | The Back Guard: Must be at the very end. | end$ | ”The end” |
\b | The Boundary: Must be a whole word. | \bcat\b | Finds “cat” but NOT “catnip” |
Wildcards
The “Shape-shifters” that can be anything.
| Symbol | Secret Job | Example | Matches |
|---|---|---|---|
. | The Secret Agent: Any single character (except new line). | c.t | cat, c!t, c9t |
\d | The Number Finder: Any single digit (0-9). | \d\d | 42, 07, 99 |
\D | The Non-Number: Anything that ISN’T a digit. | \D | a, !, G |
\w | The Word Character: Any letter, number, or underscore. | \w | a, Z, 5, _ |
\s | The Ghost: Any space, tab, or invisible character. | \d\s\d | 5 5 |
Character Sets
The “Picky Eater” rules using square brackets [].
| Symbol | Secret Job | Example | Matches |
|---|---|---|---|
[abc] | The Menu: Must be one of these three. | [cp]at | cat, pat (NOT bat) |
[a-z] | The Range: Any letter from a to z. | [a-z]! | a!, b!, z! |
[^abc] | The Opposite: Anything NOT in this list. | [^0-9] | Any non-number |
Quantifiers
The “Multipliers” that tell you how many times a symbol repeats.
| Symbol | Secret Job | Example | Matches |
|---|---|---|---|
* | The More-The-Merrier: 0 or more times. | lo*l | ll, lol, loool |
+ | The At-Least-One: 1 or more times. | lo+l | lol, loool (NOT ll) |
? | The Optional: 0 or 1 time (maybe it’s there?). | colou?r | color, colour |
{n} | The Precise: Exactly times. | \d{3} | 123, 999 |
{min,max} | The Range: Between and times. | a{2,4} | aa, aaa, aaaa |
Special Logic
The “Brain” of the pattern.
| Symbol | Secret Job | Example | Matches |
|---|---|---|---|
| | The “OR”: Matches either this or that. | cat|dog | cat, dog |
( ) | The Group: Bundles things together. | (ha)+ | haha, hahaha |
\ | The Shield (Escape): Makes a magic symbol a normal letter. | \. | Just a period . |
Dual-Agent: ^
This symbol changes jobs based on where it sits:
- Outside Brackets (
^Hello): I am the Start-of-Line guard. - Inside Brackets (
[^abc]): I am the “NOT” symbol (Opposite Day).
Examples
import re
text = "Call me at 555-1234"
pattern = r"\d{3}-\d{4}"
# Find first
match = re.search(pattern, text)
# Find all
all_matches = re.findall(pattern, text)
# Clean/Replace
clean_text = re.sub(r"[^a-zA-Z]", "", "Hello! 123") # "Hello"// cargo.toml: regex = "1"
use regex::Regex;
let re = Regex::new(r"\d+").unwrap();
let text = "I have 10 apples";
let is_match = re.is_match(text); // true
let found = re.find(text).unwrap(); // "10"Whenever you write a regex, use Regex101.com.
- Paste your text.
- Type your pattern.
- It will explain exactly what is happening in the sidebar!