Microsoft Word – Wildcard find and replace for numbers and trailing punctuation


To totally unlock this section you need to Log-in


Login

Scenario

We want to convert numbered steps like 1), 2), 3), etc. (each on a new line) to (1), (2), (3), etc. (also on separate lines). And convert numbered steps like (1), (2), (3), etc. (each on a new line) to 1., 2., 3., etc. (also on separate lines).

Notes and assumptions

  • These steps ONLY work for manually entered numbered steps, NOT automatic numbering in Word.
  • Assume that each numbered item starts a new paragraph.
  • Before you test this on your document, make a copy of the document and work on that until you are comfortable with what you have to do, and until you are satisfied that these steps are what you want.
  • Be very careful with the Replace All command — you may inadvertently replace something you don’t want to replace. Only if you are 100% sure there are no other uses of a number and the relevant trailing punctuation would you use ‘Replace All’. If you’re not sure, use Replace only. Yes, this will mean clicking ‘Replace’ for each one, but until you are confident that you won’t replace something you shouldn’t, it’s a safer option.

Example 1: Replace 1) etc. with (1)

  • Press Ctrl+H to open the Find and Replace dialog.
  • Click More, then select the Use wildcards check box.
  • In Find What, type: (^013)([0-9]@)([\)])
  • In Replace With, type: \1(\2\3

The three elements of the Find are:

  • (^013) — This represents the preceding paragraph marker for the line above the numbered step.
  • ([0-9]@) — The [0-9] represents any number from 0 to 9, and the @ represents any number of those numbers, thus not limiting the find to only single digit numbers. This finds numbers like 2, 25, and 283.
  • (\)) — You need to find a specific character (the closing parenthesis), so you need to enclose it in parentheses. However, because parentheses are special wildcard characters in their own right, you need to tell Word to treat them as normal text characters and not as special characters, so you put in a backslash ‘\‘ (also known as an ‘escape’ character) before the ).

There are no spaces preceding or trailing any of these elements, or in between them, so if you copy the code from this blog post, PLEASE get rid of any preceding spaces otherwise it won’t work.

The four elements of the Replace are:

  • \1 — Tells Word to replace the first element of the Find with what was in the Find (the paragraph marker).
  • ( — Tells Word to add an opening parenthesis before the next element (the number).
  • \2 — Tells Word to replace the second element of the Find with the same text as what was found (the numerals).
  • \3 — Tells Word to replace the third element of the Find with what was in the Find (the closing parenthesis).

Example 2: Replace (1) etc. with 1

  • Press Ctrl+H to open the Find and Replace dialog.
  • Click More, then select the Use wildcards check box.
  • In Find What, type: (^013)([\(])([0-9]@)(\))
  • In Replace With, type: \1\3.

What the find and replace "codes" mean:

The four elements of the Find are:

  • (^013) — This represents the preceding paragraph marker for the line above the numbered step.
  • ([\(]) — You need to find a specific character (the opening parenthesis), so you need to enclose it in parentheses. However, because parentheses are special wildcard characters in their own right, you need to tell Word to treat them as normal text characters and not as special characters, so you put in a backslash "\" (also known as an "escape" character) before the (, AND square brackets surrounding this string (otherwise, it won’t work).
  • ([0-9]@) — The [0-9] represents any number from 0 to 9, and the @ represents any number of those numbers, thus not limiting the find to only single digit numbers. This finds numbers like 2, 25, and 283.
  • (\)) — You need to find a specific character (the closing parenthesis), so you need to enclose it in parentheses. However, because parentheses are special wildcard characters in their own right, you need to tell Word to treat them as normal text characters and not as special characters, so you put in a backslash "\" (also known as an "escape" character) before the ).

There are no spaces preceding or trailing any of these elements, or in between them, so if you copy the code from this blog post, get rid of any preceding spaces otherwise it won’t work .

The three elements of the Replace are:

  • \1 — Tells Word to replace the first element of the Find with what was in the Find (the paragraph marker).
  • \3 — Tells Word to replace the third element of the Find with the same text as what was found (the numerals).
  • . — Tells Word to add a period (full stop) after the third element of the Find.