Find and Replace With Regex: Practical Recipes for Everyday Text

Learn when plain find and replace is enough, when to use whole-word or regex matching, and ready-to-use patterns for dates, phone numbers, spacing and lists.

Updated September 29, 2026 6 min read

Use the free Find and ReplaceFind and replace text with regex, whole word and case options

A product was renamed and the old name appears forty times in a price list. A template uses "Client Name" wherever a real name should go. A spreadsheet export writes dates as 29/09/2026, but the system you are importing into wants 2026-09-29. Editing each occurrence by hand is slow and error-prone; one missed change is enough to cause a problem.

Find and replace solves all three, but each needs a different level of precision. This guide explains the options that make replacements safe (case, whole words, first versus all), introduces regular expressions in plain language, and gives you tested patterns you can copy for common clean-up jobs.

The three levels of matching

Plain text

The simplest search finds an exact sequence of characters. Searching for cat finds "cat", but also "category" and "concatenate". That is the classic find-and-replace accident.

Whole words

Whole-word matching only finds the search term when it is not part of a longer word. cat then matches "cat" and "cat." but not "category". Use it whenever you replace a short word or an abbreviation.

Regular expressions

A regular expression (regex) is a pattern rather than fixed text. Instead of "find 2026-09-29", you can say "find any four digits, a hyphen, two digits, a hyphen and two digits". A few building blocks cover most everyday needs:

Pattern Matches
\d Any digit 0–9
\s Any whitespace: space, tab or line break
\w A letter, digit or underscore
. Any single character except a line break
+ One or more of the previous item
* Zero or more of the previous item
{2,4} Between 2 and 4 of the previous item
^ and $ Start and end of a line
( ) A group you can reuse in the replacement as $1, $2
\ Makes the next special character literal, e.g. \.

Replace text in your browser

Our Find and Replace tool highlights every match before you change anything.

  1. Open Find and Replace and paste your text into Text. Click Sample to try it with example text.
  2. Type what to look for in Find and the new text in Replace with. Leave Replace with empty to delete the matches.
  3. Set the switches: Regular expression, Case sensitive and Whole words only.
  4. Choose Replace all or Replace first only.
  5. Check the N matches found counter and the Matches highlighted panel, then compare with Result preview.
  6. Either copy or download the preview, or click Apply replacement to write the result back into the text box so you can do another replacement. Undo reverses the last applied step.

Case sensitivity is off by default, so invoice also finds "Invoice". Turn it on when capitalisation carries meaning, such as product codes or proper names. With Regular expression switched on, ^ and $ match the start and end of each line, and an invalid pattern shows a Regex error message instead of changing anything. Patterns that match an empty position rather than characters, such as ^, $ or , are counted like any other match and shown as a thin yellow bar in Matches highlighted, so you can insert text at those positions.

In plain-text mode, the replacement is inserted exactly as typed, including any $ sign, so prices like "$25" are safe.

Ready-to-use recipes

Switch on Regular expression for all of these.

Reformat dates from DD/MM/YYYY to YYYY-MM-DD

  • Find: (\d{2})/(\d{2})/(\d{4})
  • Replace with: $3-$2-$1

The three groups capture day, month and year, and the replacement reorders them.

Collapse repeated spaces

  • Find: {2,} (a space followed by {2,})
  • Replace with: a single space

If spacing is the only problem, Remove Extra Spaces does this and also handles tabs, non-breaking spaces and broken line breaks in one pass.

Delete trailing spaces at the end of lines

  • Find: [ \t]+$
  • Replace with: nothing

Add a prefix to every line

  • Find: ^
  • Replace with: -

^ matches the empty position at the start of each line, so the replacement is inserted there and the counter shows one match per line. This turns every line into a Markdown bullet list, including blank ones. To skip blank lines, use Find ^(.+)$ with Replace with - $1 instead, which only matches lines that contain at least one character.

Add a suffix to every line

  • Find: $
  • Replace with: ;

$ is the empty position at the end of each line. Combine both ideas to wrap lines, for example Find ^(.*)$ and Replace with "$1", to turn a list into quoted, comma-separated values. For richer prefix and suffix options, Add Prefix/Suffix is purpose-built.

Swap "Last, First" to "First Last"

  • Find: ^(\w+), (\w+)$
  • Replace with: $2 $1

Mask all but the last four digits of a number

  • Find: \d(?=\d{4})
  • Replace with: *

The (?=…) part is a lookahead: it matches a digit only if four more digits follow, so the last four remain visible.

Remove everything in brackets

  • Find: \s*\([^)]*\)
  • Replace with: nothing

Useful for stripping notes like "(draft)" or "(see appendix)".

For building and debugging more complex patterns with match details, the Regex Tester and our guide on testing and debugging regular expressions go deeper.

Other ways to find and replace

Word and Google Docs

Microsoft Word's Replace dialog (Ctrl+H) supports Match case, Find whole words only and, under Use wildcards, its own pattern syntax, which is similar in spirit to regex but differs in detail. Google Docs' Edit → Find and replace has Match case and Match using regular expressions. Use them when the text is already in the document and you need to keep its formatting.

Spreadsheets

Excel and Google Sheets have Find and Replace dialogs, and Google Sheets supports regular expressions there. Formulas such as SUBSTITUTE, and REGEXREPLACE in Google Sheets, apply replacements cell by cell.

Code editors and the command line

Most code editors offer regex find and replace across one file or a whole project. On the command line, sed 's/old/new/g' file.txt does the same for plain-text files. These are the right tools for replacing across many files at once.

A browser tool is handy for one-off jobs on text you have copied from anywhere, with the advantage of seeing every highlighted match before committing.

Safety tips

Preview before applying

Always check the match count. If you expected 12 matches and see 212, the pattern is too broad; add whole-word matching, case sensitivity or anchors.

Replace the longer term first

When replacing "North Region" and "North", do the longer phrase first, otherwise it will already have been changed.

Watch out for greedy patterns

.* matches as much as possible. \(.*\) on the line "(a) and (b)" matches the whole thing from the first bracket to the last. Use [^)]* or the lazy form .*? to stop at the first closing bracket.

Escape special characters

Characters such as . * + ? ( ) [ ] { } ^ $ | \ have meanings in regex. To find a literal full stop, write \.. In plain-text mode you don't need to escape anything.

Frequently asked questions

How do I replace a word but not when it is part of another word?

Switch on Whole words only. The search then matches "art" on its own but not inside "start" or "article".

How do I use capture groups in the replacement?

Put parentheses around parts of the pattern, then refer to them in Replace with as $1, $2 and so on, in the order the opening brackets appear.

Can I delete text instead of replacing it?

Yes. Leave Replace with empty and every match is removed.

Why does my regex show an error?

The pattern is not valid, often because of an unclosed bracket or a special character that needs escaping with a backslash. The error message describes the problem, and nothing is replaced until it is fixed.

Can I undo a replacement?

Yes. After Apply replacement, click Undo to restore the previous text. The tool remembers up to 20 steps.

Try it now: Find and Replace

Loading tool…