h1

Word: Wildcard find and replace for numbers and trailing punctuation

July 14, 2015

On another post on this blog, Michael commented that he had a situation where he needed to run find and replace routines (possibly using wildcards) on hundreds of documents to convert MANUALLY entered step numbers (with various trailing punctuation) to a common format ready for conversion into another type of document.

He wanted 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.
  • I have assumed 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’re 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)

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

The three elements of the Find are:

  1. (^013) — This represents the preceding paragraph marker for the line above the numbered step.
  2. ([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,
  3. (\)) — 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 (yes, I know this because it caught me out too!).

The four elements of the Replace are:

  1. \1 — Tells Word to replace the first element of the Find with what was in the Find (the paragraph marker).
  2. (  — Tells Word to add an opening parenthesis before the next element (the number).
  3. \2 — Tells Word to replace the second element of the Find with the same text as what was found (the numerals).
  4. \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.

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

What the find and replace ‘codes’ mean:

The four elements of the Find are:

  1. (^013) — This represents the preceding paragraph marker for the line above the numbered step.
  2. ([\(]) — 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).
  3. ([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,
  4. (\)) — 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. \1 — Tells Word to replace the first element of the Find with what was in the Find (the paragraph marker).
  2. \3 — Tells Word to replace the third element of the Find with the same text as what was found (the numerals).
  3. . — Tells Word to add a period (full stop) after the third element of the Find.

Michael: I hope this solves your problem. Donations to keeping this blog ad-free gratefully accepted (see the link at the top right of the page).

Update: Michael also wanted to know how to revise these find Strings if the lists used lower case letters — e.g. a), b) or (a), (b), etc. instead of numerals. In that case you’d substitute the [0-9]@ in both examples above with [a-z]. If the letters might include upper case letters, then you’d use [A-z]. Don’t forget to omit the @!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: