Find and Replace with Regex
Find and replace is the humble tool that saves hours. But most people only use the basic version. Learn the three refinements — case, whole-word and regex — and you can reshape messy text in seconds instead of editing line by line.
Start simple, but mind the traps
Plain replace swaps every occurrence of a string. The trap is partial matches: replacing art with craft also wrecks start (→ stcraft) and party. That's what the next two options fix.
Case sensitivity
By default most tools ignore case, so The and the both match. Turn on case-sensitive matching when capitalisation carries meaning — replacing a variable name in code, or fixing iPhone without touching iphone.
Whole-word matching
Whole word only matches your term when it stands alone, bounded by spaces or punctuation. Now replacing art changes the word art but leaves start and party untouched. This alone prevents most bulk-replace disasters.
Regex: matching patterns, not text
Regular expressions let you match shapes of text. A few building blocks go a long way:
\d— any digit;\d+— one or more digits.\w— a word character;\s— whitespace..— any character;*— zero or more.^and$— start and end of a line.
So \s{2,} finds runs of two or more spaces — perfect for collapsing them to one.
Capture groups: the real power
Wrap part of a pattern in parentheses to capture it, then reference it in the replacement with $1, $2. Examples:
- Reformat dates: find
(\d{4})-(\d{2})-(\d{2}), replace with$3/$2/$1. - Swap “Last, First” to “First Last”: find
(\w+), (\w+), replace with$2 $1. - Wrap numbers: find
(\d+), replace with#$1.
Test before you trust
Regex is powerful enough to mangle text spectacularly if the pattern is slightly off. Build the pattern in the regex tester first to confirm it matches exactly what you expect, then run the replacement. Our beginner's regex guide covers the syntax in depth.
Do bulk edits safely
The find and replace tool supports case-sensitive, whole-word and full regex modes with a live match count, all in your browser. For cleaning lists, pair it with remove duplicate lines.