Most of us learn how to use Microsoft Word’s Find & Replace feature early on because it’s so easy and helpful. However, sometimes a task is complex enough to send us back to the drawing board. For example, you might think replacing numbers comprised of only one or two digits impossible, but it isn’t. Thanks to a few wildcards, you can accomplish this in only one run. In this article, I’ll show you the wildcard string that will find all numbers consisting of only one or two digits.
SEE: Software Installation Policy (TechRepublic)
I’m using Microsoft 365 on a Windows 10 64-bit system, but you can use an earlier version. I recommend that you hold off on upgrading to Windows 11 until all the kinks are worked out. For your convenience, you can download the demonstration .docx file. However, it’s a simple list of one-, two- and three-digit numbers that you can easily create yourself.
The Replace string
Let’s keep the Replace string simple. Specifically, let’s add text to the beginning and the end of each number. Doing so lets us see where things work and don’t work. We’ll be working with a simple set of numbers, shown in Figure A. The simple list contains single-, double- and triple-digit numbers.
Figure A
We want to ignore any number comprised of more than two digits. To that end, we’ll work with the following replace string:
pre^&sub
If Word finds the number 03, this Replace string will return pre03sub.
How to find numerical digits
To replace or modify existing numerical values comprised only of one or two digits, you need wildcards. You could consider simplifying the task by running two different replace tasks: one that finds only single-digit numbers and one that finds only double-digit numbers. Fortunately, you can easily combine both searches into one.
The first wildcard component we’ll try is [0-9], which finds any digit.
Now, let’s work through the first Find string:
- On the Home tab, click the Editing option and click Replace. Or press Ctrl + h.
- In the resulting dialog, enter [0-9] as the Find What string.
- Enter pre^&sub as the Replace With string.
- If necessary, click More.
- Check the Use Wildcards option shown in Figure B.
- Click Replace All and then OK to close the information prompt.
Figure B
As you can see in Figure C, this Replace task added the Replace With string to every digit, not every number. That means we need a wildcard that matches a single word. In this case, word includes numbers. If you’re working with the demonstration file, be sure to press Ctrl+Z to undo the changes made by running Replace before you continue to the next wildcard example.
Figure C
Fortunately, there is a wildcard that can handle this particular find requirement: <>. By enclosing the [0-9] string in <> characters, Word will interpret combined digits as a whole word. Let’s add the <> characters and try again.
- Press Ctrl + H.
- The Replace feature remembers the last task, so you don’t have to enter the strings from scratch. Instead, insert the < and > characters, as shown in Figure D.
- Click Replace All and OK.
As you can see in Figure D, we’re a bit closer. Word ignores the three-digit numbers, but it also ignores the double-digit numbers. Don’t forget to press Ctrl + Z to undo the changes before you continue.
Figure D
We need a way to specify double-digit numbers while ignoring numbers with more than two digits. That’s where the next wildcard comes in: {}. This wildcard specifies any sequences of digits, which you specify. {1,} would find single-digit numbers; {1,2} will find only single- and double-digit numbers. Let’s add it to our existing Find What string and see what happens:
- Press Ctrl + H.
- Tack the {1,2} wildcard component to the end of the Find What string, as shown in Figure E.
- Press Replace All and then OK.
Figure E
Success! This Find What string meets every requirement. Let’s review the wildcards we used and what they do.
A quick review of wildcards in Word
Let’s take a minute to review what each wildcard does:
- [0-9] specifies all digits. If you used [0-3], Word would find only the digits 0 through 3.
- <> limits the search to single words. Subsequently, <[0-9]> won’t treat numbers consisting of more than one digit as a bunch of individual digits.
- {} lets you specify a sequence of digits; {1,2} will find numbers that consist only of one or two digits.
By combining these three wildcards, <[0-9]{1,2}> you’ll find only numerical values that consist of one or two digits. Once you know how the wildcards work, it’s easy to come up with this string so you can run only one Replace task.