Word: More find and replace with wildcardsApril 20, 2012
I needed to make a global change to some text that the author had written incorrectly (i.e. not according to our house style guide).
They had written [Ref 1] for a reference instead of our style of [Ref. 1], which not only has a full stop (period) after the f but also uses a non-breaking space to separate the full stop and the following numeral. All the numbers were different. Some were a single number, while those from References 10 and higher were two-digit numbers. There were a LOT of references like this in the document.
So I used my knowledge of find and replace wildcards to globally make the change in just a few seconds.
This solution works in Word 2003, Word 2007, and Word 2010.
- In the Word document where you want to make this change, press Ctrl+H to open the Find and Replace dialog box; the Replace tab should be in focus.
- In the Find what field, type: (Ref)( )([0-9])
Note: There is only ONE space in this string — it’s between the two parentheses that don’t appear to enclose anything. The 0-9 bit is a zero, not an ‘o’ for orange.
- In the Replace with field, type: \1.^s\3
Note: There are NO spaces in this string and the s must be in lower case. Don’t forget the full stop!
- Click the More button.
- Select the Use wildcards check box.
- Click Find Next then click Replace to test that it works fine. If so, click Replace All.
Explanation for how this works:
- (Ref) looks for the string of letters: Ref
- ( ) looks for a space immediately after Ref (i.e. no punctuation)
- ([0-9]) looks for any number that follows immediately after Ref and its following space. The square brackets indicate a range — in this case any number from 0 to 9 will be found. It doesn’t matter whether the numbers are one or two-digit numbers — the critical thing the find/replace is looking for is any numeral after Ref<space>.
- \1 replaces the first part of the Find string with itself (in other words, Ref gets replaced with Ref)
- .^s replaces the second part of the Find string (the space) with a full stop followed immediately by a non-breaking space. For a non-breaking space, you MUST use a lower case s and precede it with the ^ (Shift+6). Note: If you just want an ordinary space — not a non-breaking one — then use \2 instead of .^s.
- \3 replaces the third part of the Find string with itself (in other words, the number found gets replaced with the same number).