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 @!

7 comments

  1. Probably one of the most helpful sites for the ubiquitous but nit-picky WORD find and replace dialog unless you have all the pieces parts memorized which I don’t. I do a lot of pdf conversions which are difficult because sentences are often broken mid line with paragraph marks and numbered lists also give me fits. Actually trying to change anything at the beginning of a sentence/paragraph seems tricky. Current problem is replacing-reformatting a single capital letter on its own line. The current solutions from the web use [^13] are for the start of a new line or paragraph but it also wants to grab and format the preceding paragraph mark. How do you select just the first character on a line and does it matter if is part of a sentence or a paragraph? Also how would you select a paragraph mark that ISN’T preceded by a period? Thanks for the great site.


  2. Hi Lauren

    You can select a standard paragraph mark by using ^p in a standard find and replace (not wildcards). So, if you had multiple (empty) paragraphs one after you could enter ^p^p^p (for 3 paragraphs) and replace with ^p (one paragraph). This doesn’t select the preceding period.

    If you had several spaces before a paragraph mark, then you’d type say three spaces, then ^p in the Find, then replace with just ^p.

    –Rhonda


  3. I have a question, how to replace the space after numbers with . or –
    for example 1 text with 1. text or 1- text
    thanks


  4. Hi, I have written percentage in my report enclosed as (numeric in parenthesis) i.e. i need a wildcard replace code for : (56%) to 56%

    Can someone help me, thanks in advance

    AVi


  5. […] a comment on another post (https://cybertext.wordpress.com/2015/07/14/word-wildcard-find-and-replace-for-numbers-and-trailing-p…), AVi asked if there was a way to find percentage numbers (e.g. 56%) that were inside parentheses, […]


  6. Hi AVi

    I’ve written up a solution for you here: https://cybertext.wordpress.com/2018/07/22/word-wildcard-find-and-replace-for-numbers-inside-parentheses/

    –Rhonda


  7. I have a paragraph consisting of many paragraphs and multiple lines, I am scanning a block of several lines? How to count the number of lines I have selected? thanks you.



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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: