fixmint

Find and replace without breaking text: whole words, case and regex

· 3 min read

To find and replace text safely, look at the matches before you copy the result. A plain replace changes every occurrence, including the ones inside longer words, so turn on whole-word matching for names and short words, and check that the match count is the number you expected.

Find and replaceReplace a word everywhereOpen

Why a plain replace breaks text

Take the sentence "The category cat sat" and replace cat with dog. A plain replace changes every run of the letters c, a, t, including the start of category, and the result is "The dogegory dog sat". The tool has done what the rule asked, which was to change every match.

With Whole word only ticked, a match must not sit inside a longer word. The same replacement gives "The category dog sat". Letters and digits from any alphabet count as parts of a word, so caf does not match inside café.

Whole-word matching is the right default for names, short words and abbreviations. Turn it off when you want partial matches, such as changing a prefix in a list of product codes.

Match case

Matching ignores case by default, so Cat, cat and CAT all match. That helps when one word is typed in different cases, but it can also change a word you wanted to keep. A name like Bill and a common word like bill would both match.

Turn on Match case when the capital letter tells you what the word means. Use the highlighted preview to see exactly which words the rule would change.

Regular expressions and groups

With Regex on, the Find box is read as a pattern rather than plain text. Parentheses capture part of a match, and the Replace box can use the captured text as $1, $2 and so on. Find (\w+)@(\w+) with Replace $2 at $1, and the text ana@site becomes site at ana. The code $& stands for the whole match.

Special characters change meaning in the same way. Outside Regex mode, a dot is a dot. Inside Regex mode, a dot matches any single character, so the pattern 1.5 also matches 1x5. If you are not sure whether the text contains special characters, leave Regex off and type it exactly.

The dollar sign in the Replace box

Outside Regex mode, the Replace box is used exactly as typed, so $5 is inserted as $5. With Regex on, a dollar sign has a meaning: $1 refers to a group, $& to the whole match, and $$ gives one dollar sign. To insert the text $5 with Regex on, write $$5.

Line breaks and tabs

To put a line break or a tab into the Replace box, turn on Read \n and \t in Replace. The two codes then insert a line break and a tab. To find a line break, turn on Regex and type \n in the Find box, because a plain Find box treats \n as two characters.

Check before you copy

Look at four things before you copy the result:

  • The match count. If you expected 12 changes and the tool shows 40, a word is probably matching inside another word.
  • The highlighted preview. It marks every match in your original text, including the ones you did not want.
  • The result. Read the sentences around each change, not only the changed word.
  • The original. Keep your starting text. The tool keeps no history, so the original is your only undo.

For a long document, paste the old and new versions into compare text. It lists every changed line, and marks the changed words inside each line.

Edge cases

  • Empty Replace box. The matches are deleted. This is useful for removing a stray word, but check the count first.
  • A replacement that contains the match. The replacement runs once over the original text. Replacing a with aa in banana gives baanaanaa, and nothing loops.
  • Invalid patterns. An unfinished group such as (abc is reported in a short message, and no result is shown until the pattern is valid.
  • Very many matches. The preview highlights the first 5,000 matches, but every match is still replaced.

Quick answers

Why did my replace change words inside other words?

Whole word only was off. Turn it on, then check the count against what you expected.

Why does a dot match the wrong text?

Regex is on, so the dot matches any character. Turn Regex off for plain text, or write \. to match a real dot.

How do I find a line break?

Turn on Regex and type \n in the Find box.

Cover of How to Remain Valuable When Intelligence Becomes CheapHow to Remain Valuable When Intelligence Becomes Cheap

224 pages on the human, economic and strategic advantages that stay scarce when AI can do most cognitive work.

$3.84 Get it on Gumroad →