Regex search and replace

Placeholder

Written By Logiks Solutions

Last updated 1 day ago

What this does

Task menu ▸ Word HelpersRegex Search & Replace

Searches with regular expressions – patterns instead of fixed strings. That lets you find every date, every reference number of a given shape, or every decimal figure in one pass, and replace them.

Word has its own wildcard search which works differently and does less. This feature uses the common regex syntax.

First: save

A pattern that matches too much changes hundreds of places in one go. Save first or work in a copy. You do not see the matches individually for confirmation.

Step 1: the patterns

You enter your patterns, one per line. For the syntax the add-in points you to vischerlnk.com/regexinfo.

Then the options, as letters:

  • i – case is ignored
  • m – multiline: ^ and $ apply per line
  • s – singleline: the dot also matches line breaks
  • c – compiled (faster with many matches)
  • r – right to left
  • e – capture named groups only

Step 2: the replacements

One replacement text per line, in the same order as the patterns. If the line counts do not match, the add-in aborts without changing anything – a sensible safeguard.

Leave the step empty or cancel it and it only searches, jumping to the first match. That is exactly how you try a pattern out before replacing with it.

The scope

If you have text selected, only that is worked on; otherwise the whole document. The feedback tells you both – how many replacements and where.

A practical routine

  1. Enter the pattern, cancel the replacement step. Search only.
  2. Look at what was matched. If it is wrong: sharpen the pattern.
  3. Only then run it with replacements.

Error messages

  • "Your regex pattern '…' is invalid" – a mistake in the pattern; nothing is replaced.
  • "The number of regex patterns does not match the number of replacement lines." – usually a stray empty line.
  • "No matches found for '…'." – the pattern matches nothing in the chosen scope.