What regex find and replace does
Plain search finds exact text. Regex finds a shape of text. Instead of searching for the word 2026, you can search for any four-digit number. Instead of one spelling, you match a whole family of matches. This is why regex is the power tool of find and replace.
A regular expression is just a short pattern made of ordinary characters plus a few special symbols. Once you learn a handful of those symbols, you can clean and reformat text that plain search cannot touch. Everything below runs on your device in the Find & Replace tool.
The patterns you will actually use
You do not need to memorize the whole regex language. A small set of patterns covers most real jobs.
| Pattern | Matches | Use |
|---|---|---|
| \d | any digit | numbers, IDs |
| \s | any whitespace | spaces, tabs, newlines |
| \w | word character | letters and digits |
| + | one or more | repeat the last pattern |
| \b | word boundary | whole-word matching |
Combine building blocks. The pattern \d+ matches a whole number of any length, and \s+ matches a run of any whitespace, perfect for collapsing gaps.
Step-by-step regex replace
- Turn on regex mode in the Find & Replace tool.
- Write a small pattern that describes your target.
- Test it on a short sample first.
- Write the replacement, using group references if needed.
- Run it on the sample and confirm the output.
- Apply to the full text and review the result.
Never run an untested regex on important text. A greedy or slightly wrong pattern can change far more than you intended, and undoing it is hard.
Using capture groups
Capture groups let you keep part of a match and reuse it in the replacement. You wrap part of the pattern in parentheses, then reference it in the replacement.
Reordering a date
Suppose dates read YYYY-MM-DD and you want DD/MM/YYYY. Capture the three parts, then reorder them in the replacement. One expression converts every date in the list.
Wrapping matched text
You can wrap every matched term in quotes or brackets by capturing it and surrounding the reference with the characters you want. This is handy for turning a plain list into a quoted one.
| Goal | Idea | Result |
|---|---|---|
| Reorder date | capture 3 parts, swap order | 2026-07-25 to 25/07/2026 |
| Quote each item | capture item, add quotes | apple to "apple" |
| Swap name order | capture first and last | Jane Doe to Doe, Jane |
Real cleanup examples
Collapse extra whitespace
Replace \s+ with a single space to fix messy spacing from a bad copy-paste. This is one of the most reused patterns in daily work.
Strip trailing spaces
Match spaces before each line end and remove them. This tidies diffs and prevents invisible whitespace bugs in code and data.
Turn a list into CSV
Replace line breaks with commas to convert a vertical list into a single comma-separated line. Afterward, dedupe with remove duplicate lines before you convert, or remove duplicate emails for contact lists.
Regex vs plain replace
Regex is powerful but not always the right choice. For a single fixed word, plain replace is simpler and safer.
| Situation | Plain replace | Regex |
|---|---|---|
| One exact word | Best fit | Overkill |
| Many similar patterns | Tedious | One expression |
| Reordering parts | Not possible | Capture groups |
| Whitespace cleanup | Hard | Simple pattern |
For a few unrelated words, see replace multiple words at once. For patterns, regex wins every time.
Best practices for safe regex
- Always test on a small sample before the full text.
- Prefer specific patterns over broad ones.
- Watch for greedy matching that grabs too much.
- Keep a backup of the original before running.
- Verify the result with the text diff checker.
Build your pattern piece by piece. Start with a simple match, confirm it, then add repetition and groups. Small steps catch mistakes early.
Common regex mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Greedy quantifier | Matches too much | Use precise patterns |
| Forgot to escape | Special char misread | Escape . and * |
| No sample test | Surprise damage | Test small first |
| Ignoring boundaries | Partial-word matches | Add \b |
For errors across all replace types, read common find and replace mistakes, and explore all tools to pair regex with cleanup helpers.
