Advanced Find/Replace in Word

It happens to everyone from time to time: when typing the umpteenth word 'XYZ' in a lengthy text, they realize that 'ABC' is actually better. The search and replace function is then indispensable, and guarantees that no 'XYZ' is overlooked. However, for some ingenious search situations, the solutions are well hidden or require a gentle form of user virtuosity. And then this Expert course comes in handy!

This article consists of two pages:

Page 1 (current page)

- Via the menu or the keyboard

- Wildcards

- Special situations

- From wildcards to regular expressions

Page 2

- Find and replace formatting

- Superscripts

- Replace text

- Find/replace as abacus

Via the menu or the keyboard

Both the Find and Replace functions in Microsoft Word 2007 are available under the Edit button of the Home tab on the Ribbon (in Word 2003, go to the Edit menu). In both Word versions, the search/replace function is easier to call up with these shortcuts: Ctrl+F to find, Ctrl+H to replace. Don't worry if you get these mixed up: the same dialog box is shown each time, but with a different active tab. So one extra click is enough to select the right one.

Depending on the length of the ribbon, the button that gives access to the search function changes.

wildcards

In both a search and a replace operation, you can use so-called wildcard characters in the Search for box. That you intend to do this, you must first indicate with the checkbox of the same name under the search options, hidden behind the More >> button. From then on, typing ?culture produce both 'culture' and 'culture'. Search < ?culture finds the loose word 'culture', but then skips 'youth culture' again. Looking up k[ia]st finds "chest" and "closet", but skips "coast" or "cost". Of k[!a-n]st it is the other way around: 'closet' and 'chest' are not found, because the 'a' and the 'i' belong to the series 'a-n', a series excluded by the preceding exclamation mark. If we want to find 'series 1', 'series 2' through 'series 5', but we are not interested in 'series 6' and following, try using series [1-5] in the search box. One precaution with both techniques: the strings between the square brackets must be in ascending order. So don't try to search for series [5-1] because this just returns an "invalid range" error. Another example: searching for 10{1,2}> finds '10' and '100', but not '1000' and beyond. Don't forget the greater-than wildcard (>) at the end, otherwise 1000 will still be found based on its first three digits.

When wildcards?

Most escape codes have no effect if the Use wildcards option is checked. At the frequently used paragraph mark (^p) that is the case. Other codes require that this option is activated: this is how you search with ^m both to manual page breaks and to section breaks. Doesn't work: just try and wait for the error message...

Special situations

Sometimes we want to look for signs that are in a special situation. Suppose we want to double the number of blank lines between paragraphs. Then we need to find where we ended a paragraph by pressing the Enter key. To this end, we place ^p in the search box. When in doubt, there is the Special button at the bottom of the tab: this opens a menu, in which we find (among other things) the slightly more expressive option Paragraph marking that automatically ^p fills in. We call these codes escape codes and may be used in both the search and replace fields. So it suffices to search for ^p and replace it with ^p^p.

To show such (normally invisible) characters on the screen, in Word 2007, on the Home tab of the Ribbon, go to the Paragraph menu and click the Show All button (the -symbol). In Word 2003, searches the old-fashioned toolbar for the symbol, which is called the Show/Hide button there. Or use the key combination Ctrl+Shift+8 in both versions. Search ^p leave the search function at each paragraph mark (the -sign) stop.

From wildcards to regular expressions

Regular expressions ('regular expressions' or 'regex' for short) are special combinations of characters and wildcards, which gain power through careful selection and can thus detect and replace complex text patterns. Suppose we have a long list of addresses, in which the name of the street is followed by the house number, and we want to convert it into a series in which we get the house number first, followed by a comma and only then the street name. In this example, the address 'Richard Holkade 8' should be converted to '8, Richard Holkade'. Let's assume for a moment that each address line was entered as a separate paragraph.

We open the search/replace window, indicate that we want to work with wildcards, and tap (*)([! ]@)^13 in as string to search. In it, the first pair of parentheses includes a matching expression, which may consist of any characters (the asterisk), followed by a second expression that ends at the end line (^13) and from which we exclude the starting space ([! ]). That way we don't unnecessarily include it in the replacement text.

In the Replace with box, we type: \2, \1^p. Of \1 and \2 we search for the isolated expressions: we indicate that we want to place the second string found first, and only then the first, after a comma and a space as separator. We also close each substitution with an Enter key (^p).

Regular expressions look cryptic, but allow for complex substitutions.

One paragraph break is not the other

To indicate the end of a paragraph (the place where the author pressed the Enter key), we may use both the code ^13 (think of the ASCII code for 'carriage return') if ^p (with the p of 'paragraph'). But there is a difference between the two: ^p contains formatting information, ^13 not. In addition, works ^p not in wildcard searches. So use ^13 to search for paragraph breaks, but prefer ^p in the replacement box.

Recent Posts

$config[zx-auto] not found$config[zx-overlay] not found