Some Microsoft Word users never progress beyond the simplest find-and-replace tasks because they don’t know about wildcards or they find them difficult to apply. I admit that sometimes the solution remains a mystery to me until I leave the computer to run the vacuum or perform some other menial task, which seems to clear my mind so the answer can rise to the surface.
SEE: 83 Excel tips every user should master (TechRepublic)
Knowing how the wildcards work isn’t enough; you must be aware of how the two search strings, find and replace, relate to one another. In this article, I’ll show you a replace task that uses wildcards to insert a space in the middle of three characters. How they relate to each other is necessary to succeed.
I’m using Microsoft 365 on a Windows 10 64-bit system, but you can use earlier versions. Word Online doesn’t support wildcards. For you convenience, you can download the demonstration .docx and .doc files, but the search strings are simple, and you can easily create the document yourself by referring to Figure A.
Let’s suppose you have several strings where the only relationship is that the first two characters are always the same—let’s use a and b. What follows ab will often be different: abc, ab8, abf and so on. The characters following the first two are inconsistent in type but the strings will always be three characters, as shown in Figure A. You want to retain all of the characters but insert a space after b, in every instance.
In Excel, you might try a string function or even Text to Columns, but those features aren’t available in Word. Your first inclination is probably Replace, and that’s where I would start.
Unless you’re very familiar with this feature, you might try a few things that won’t work such as ab?, which works OK for finding the first three characters in strings that begin with ab, but how do you retain the ? character in the replace string? If you try ab ?, you’ll find that it doesn’t work as expected! It returns, ab ?; it doesn’t retain the actual third character in the string. You might try several different sets of find-and-replace strings that almost get you there. The answer is a few wildcards and is probably easier than you might think.
How to use the wildcards in Word
As hinted at, wildcards are the answer, but getting just the right ones in both the find-and-replace search strings takes a bit of thought (unless you’re a wildcard expert). Let’s break it down by each wildcard character.
First, the ? character will find any single character, so ab? does what you expect. The problem is, you can’t easily express the replacement string, not as is. That solution is to define a range using the () characters so Replace knows which character to copy to the replacement string, after inserting the space:
This gets us closer because the range (?) is referred to in the replace string by position. In this case, it’s the first position, so you end up with
replace: ab 1
The replace string includes a space between ab and 1, but what does 1 mean? That’s a reference to the region (?) in the find string. Simply put, the character references a range and the number 1 tells Word which range. In this case, there’s only one. Word copies the character in parentheses to the replacement string. These two get us almost there, so let’s run them and see what happens:
- Press Ctrl + H to open the Replace dialog.
- Delete any previous find-and-replace strings.
- In the Find What control, enter ab(?).
- In the Replace With control, enter ab 1. Remember that there’s a space between ab and 1.
- If necessary, click the More button and then check the Use Wildcards option and the No Formatting option if it isn’t dimmed (to remove formatting applied in the previous replace task).
- Click Replace All (Figure B).
- Close the dialog.
As you can see in Figure B, the find-and-replace strings worked perfectly until they encountered the word ‘about.’ That’s because the (?) component does match a single character, but it doesn’t match only one character because it’s part of a range. Consequently, it doesn’t reduce the find string to only three characters.
The good news is that we truly are almost there. Before continuing, press Ctrl + Z to undo all the changes the previous replace task made. Then, repeat the instructions above, skipping step 2—you don’t want to delete what you already have. In step 3, enter > to the end of the string: ab(?)>. The > character denotes the end of the search. Adding this character will force Word to stop matching with the third character.
As you can see in Figure C, the search works as expected, changing the first three strings and leaving the word “about” intact.
Are you wondering why Word ignores ABC, Inc.? Checking the Use Wildcards option takes care of that string automatically because wildcard searches are case sensitive.
We used only a few wildcards—(), ?, and —to get the job done. Knowing what wildcards do and how they work together is key to success.
Subscribe to the Developer Insider Newsletter
From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays