When a Replace task goes beyond the basics, it requires wildcards and specialized knowledge. Learn how to use specific wildcards to limit the number of characters in a search string.
Microsoft Word's Replace feature is a powerful tool that can take on just about any search task. The problem is that it is so powerful that users have trouble applying it beyond the basics. It's easy to replace a literal value with another, but once you throw in a few conditions, the task becomes more complex. For example, do you know how to limit the number of characters in a search string? Do you know how to replace one or more characters in a search string while leaving other characters alone? In this article, we'll tackle both problems in the same search task. Even if you're an expert, you might learn something new.
SEE: 69 Excel tips every user should master (TechRepublic)
I'm using Microsoft 365 on a Windows 10 64-bit system. You can work with your own data or follow along by using the downloadable demonstration .docx or .doc file. You can't use wildcards in an online document search. This article assumes you have basic Word skills.
How to use the Replace feature in Word
At first, the Find and Replace task we're going to explore sounds easy. Let's suppose that you have a Word document with multiple three-digit numbers starting with 3, and you want to change the 3 to a 5. For example, 352 becomes 552, 301 becomes 501, 399 becomes 599, and so on. As usual, there's an easy way, but that way doesn't limit the number of digits (or characters) in the search string. When that's the case, there's a more complex set of find and replace settings that will limit the task to only three-digit numbers. Now, let's get started!
The easy way to replace a character in Word
When replacing only characters at the beginning of the search string, you can use the Match Prefix option. This option matches words that begin with the Find setting; you can replace that character with another character without changing the remaining characters. Figure A shows a simple document with only a few numbers. Let's see what happens when we use this option in this document:
- Press Ctrl+H to open the Replace dialog.
- Click More and at the bottom click No Formatting. If that option is dimmed, you can skip this step.
- Click inside the Find What control and remove its contents if necessary, before entering 3.
- Click inside the Replace With control and remove its contents if necessary, before entering 5. (See Figure A.) It's important to note at this point that Replace remembers the last task. That's why it's important to remove formatting and previous search and replace strings before beginning.
- In the bottom pane, check the Match Prefix option.
- Click Replace All, click OK to confirm and then close the dialog.
The results are shown to the right in Figure A. As you can see, this task replaces all 3s with 5s when the 3 occurs at the beginning of the number. It doesn't consider the number of digits in the number.
This easy solution might work, but if you need to limit the change to numbers of a specific number of digits, you'll need a different replace task.
How to use wildcards to replace a character in Word
When the Match Prefix option isn't restrictive enough, you can still use Replace, but both settings will require wildcards. Let's restate the problem: We want to replace 3 with 5 when 3 occurs at the beginning of a three-digit number. You can accomplish this with the following settings:
- Find What: <3([0-9])([0-9])>
- Replace With: 5\1\2
Let's work through the same example and then discuss how it works. First, the steps:
- Press Ctrl+H to open the Replace dialog.
- Click More and at the bottom if necessary. (Remember to click No Formatting in the future; it isn't necessary this time.)
- Click inside the Find What control and remove its contents if necessary, before entering <3([0-9])([0-9])>.
- Click inside the Replace With control and remove its contents if necessary, before entering 5\1\2. (See Figure B.)
- In the bottom pane, uncheck the Match Prefix option and check the Use Wildcards option.
- Click Replace All, click OK to confirm and close the dialog.
As you can see in Figure C, the settings worked! The Find What setting matches all three-digit numbers that begin with 3. The Replace With setting replaces the 3 with 5. There are only two three-digit numbers that begin with 3 and after the task, they begin with a 5. None of the other numbers are changed.
At this point, it's time to learn how the settings work. First, let's break down the Find What setting:
- The < character denotes the beginning of the search string and > denotes the end.
- The first component <3 matches only those strings that begin with 3.
- The () characters represent a range. In this case, ([0-9]) will match any number in the second position. Similarly, the same component will find any number in the third position.
- Using > forces Word to stop searching beyond the third digit. <3 is the first digit, ([0-9]) is the second digit and ([0-9]) is the third digit.
Put it all together and you get a search string that specifies a three-digit number that begins with 3. Now let's review the Replace With setting:
- 5 is a literal character in the first position, and as such, replaces 3, the first literal character in the Find What setting.
- The \ character allows you to specify a range—remember, that's the wildcards enclosed in brackets ([ ]). In this case we have two ranges: ([0-9]) in both the second and third positions, but they are ranges 1 and 2, not 2 and 3—don't confuse the positions of the character within the string with the range. Remember, <3 isn't a range, it's a literal character.
This setting specifies a 5 as the replacement for the first digit, followed by the original digits in the matched number, which will only have three digits.
More tips on how to use the Replace feature in Word
It seems complex, but once you know what all those wildcards do, it's easy to understand. I'm not convinced that less complex settings won't work, but I've yet to discover them.
If you can't work out a one-step set, try two. Replace the character, in this case, the number 3, with a special character that you're not using in the document, such as the ^ sign or an underscore (_). Then run a second task to replace the special character with 5. It really isn't a lot more work, and if you're not all that knowledgeable with wildcards, it might be quicker than trying to work out a one-step wildcard solution.
- Microsoft 365: A cheat sheet (TechRepublic)
- Zoom vs. Microsoft Teams, Google Meet, Cisco WebEx and Skype: Choosing the right video-conferencing apps for you (free PDF) (TechRepublic)
- Checklist: Securing Windows 10 systems (TechRepublic Premium)
- Everything you need to know about the Microsoft Exchange Server hack (ZDNet)
- Must-read coverage: Windows 10 (TechRepublic on Flipboard)