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.

SymbolSecret JobExampleMatches
^The Front Guard: Must be at the very start.^HelloHello there”
$The Back Guard: Must be at the very end.end$”The end
\bThe Boundary: Must be a whole word.\bcat\bFinds “cat” but NOT “catnip”

Wildcards

The “Shape-shifters” that can be anything.

SymbolSecret JobExampleMatches
.The Secret Agent: Any single character (except new line).c.tcat, c!t, c9t
\dThe Number Finder: Any single digit (0-9).\d\d42, 07, 99
\DThe Non-Number: Anything that ISN’T a digit.\Da, !, G
\wThe Word Character: Any letter, number, or underscore.\wa, Z, 5, _
\sThe Ghost: Any space, tab, or invisible character.\d\s\d5 5

Character Sets

The “Picky Eater” rules using square brackets [].

SymbolSecret JobExampleMatches
[abc]The Menu: Must be one of these three.[cp]atcat, 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.

SymbolSecret JobExampleMatches
*The More-The-Merrier: 0 or more times.lo*lll, lol, loool
+The At-Least-One: 1 or more times.lo+llol, loool (NOT ll)
?The Optional: 0 or 1 time (maybe it’s there?).colou?rcolor, 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.

SymbolSecret JobExampleMatches
|The “OR”: Matches either this or that.cat|dogcat, 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:

  1. Outside Brackets (^Hello): I am the Start-of-Line guard.
  2. 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.

  1. Paste your text.
  2. Type your pattern.
  3. It will explain exactly what is happening in the sidebar!