Find and replace text and other data in a Word document
Word offers several options for locating specific content in your document. You can search for and replace items such as text, images, captions, bookmarks, or certain types of formatting such as paragraphs or page breaks. You can use the Go To command to go to a particular instance of content in your document, and you can also extend your search by using wildcards, codes, or regular expressions to find words or phrases that contain specific characters or combinations of characters.
Notes:
-
Word Online currently includes only basic find and replace features. Open your document in the Word desktop application to use the advanced find and replace features, such as finding objects, finding formatting, using Go To, and using wildcards, codes, and regular expressions.
-
Some of the content in this topic may not be applicable to some languages.
Find and replace
Find text
You can quickly search for every occurrence of a specific word or phrase.
-
On the Home tab, in the Editing group, choose Find. Or press Ctrl+F on your keyboard.
The Navigation pane is displayed.
-
In the search box, type the text that you want to find. Any instances of the found text are highlighted in the document and listed in the Navigation pane.
-
Browse through the results by using the up and down arrow controls.
Note: If you make a change in your document and the search results disappear, click the down arrow below the search box to view the list of results again.
Find and replace text
-
On the Home tab, in the Editing group, choose Replace. Or press Ctrl+H on your keyboard.
-
In the Find what box, type the text that you want to search for and replace.
-
In the Replace with box, type the replacement text.
-
Choose Find Next, and then do one of the following:
-
To replace the highlighted text, click Replace.
-
To replace all instances of the text in your document, click Replace All.
-
To skip this instance of the text and proceed to the next instance, click Find Next.
Note: In Word Online, there is no Find Next button. Browse through the results by using the up and own arrows to replace instances of highlighted text.
-
Find images, tables, comments, and other objects
To search for objects like tables, graphics, comments, footnotes, endnotes, or equations, do the following:
-
On the Home tab, in the Editing group, click Find. Or press Ctrl+F on your keyboard.
The Navigation pane is displayed.
-
Click the arrow on the right side of the search box, and then choose an option from the list.
The search results are displayed below in the search box.
-
Select a result to see it in your document, or browse through all the results by clicking the arrow controls.
Looking for the Find and Replace dialog box?
To perform an advanced search by using the Find and Replace dialog box, do one of the following:
-
On the Home tab, in the Editing group, choose the arrow next to Find, and then select Advanced Find.
or
-
In the Navigation pane, click the arrow on the right side of the search box, and then choose Advanced Find.
In the Find and Replace dialog box, click More for detailed search options.
Note: For a smaller set of advanced search options, such as Match case or Find whole words only, in the Navigation pane, select the arrow on the right side of the search box, and then choose Options.
Find and replace specific formatting
You can search for and replace or remove character formatting in your document. For example, you can search for a specific word or phrase and change the font color, or you can search for specific formatting, such as bold or highlight, and change it.
-
On the Home tab, in the Editing group, choose Replace. Or press Ctrl+H on your keyboard.
-
If you don't see the Format button, click More.
-
To search for text with specific formatting, type the text in the Find what box. To find formatting only, leave the box blank.
-
Click Format, and then select the formats that you want to find and replace. For example, to find highlighted text, click Format > Highlight; to find bold text, click Format > Font, and then in the Find Font dialog box, select Bold in the Font style list.
-
Click the Replace with box, click Format, and then select the replacement formats as described in step 4.
Note: If you also want to replace the text, type the replacement text in the Replace with box.
-
To find and replace each instance of the specified formatting, click Find Next, and then click Replace. To replace all instances of the specified formatting, click Replace All.
Find and replace paragraph breaks, tabs, page breaks, and other items
You can search for and replace special characters and document elements such as tabs, paragraph marks (carriage returns), and manual page breaks. For example, you can find all double line spaces (two paragraph marks) and replace them with single line spaces (one paragraph mark).
Tip: To show hidden formatting symbols, such as spaces, paragraph marks, and page breaks, click ¶ in the Paragraph group on the Home tab.
-
On the Home tab, in the Editing group, click the arrow next to Find, and then click Advanced Find.
-
If you don't see the Special button, click More.
-
Click Special, and then click the item that you want.
-
If you want to replace the item, click the Replace tab, and then type what you want to use as a replacement in the Replace with box.
-
To find and replace each instance of the specified item, click Find Next, and then click Replace. To replace all instances of the specified item, click Replace All.
Find by using Go To
Go to a specific page, table, heading, line, or other item
-
On the Home tab, in the Editing group, click the arrow next to Find, and then click Go To.
-
In the Go to what box, click the type of item.
-
Do one of the following:
-
To go to a specific item, type the appropriate identifying information for the item in the Enter item type box, and then click Go To.
-
To go to the next or previous item of the specified type, leave the Enter box empty, and then click Next or Previous.
-
Search by using wildcards
Search by using wildcards to find specific letters
Use wildcards to search for text. For example, you can use the asterisk (*) wildcard to search for a string of characters (for example, "s*d" finds "sad" and "started").
-
On the Home tab, in the Editing group, click the arrow next to Find, and then click Advanced Find.
-
Select the Use wildcards check box.
If you don't see the Use wildcards check box, click More.
-
Do one of the following:
-
To choose a wildcard character from a list, click Special, click a wildcard character, and then type any additional text in the Find what box. For more information, see the table Wildcards for items you want to find and replace, below.
-
Type a wildcard character directly in the Find what box. For more information, see the table Wildcards for items you want to find and replace, below.
-
-
If you want to replace the item, click the Replace tab, and then type what you want to use as a replacement in the Replace with box.
-
Click Find Next or Find All and then click Replace, or click Replace All to replace all instances of the item.
Note: To cancel a search in progress, press ESC.
Expressions in wildcard searches
Use parentheses ( ) to create groups of wildcard characters and text in the Find What box, and then use \n in the Replace With box to use the results of each expression.
You can use the \n wildcard to search for an expression and then replace it with the rearranged expression. For example, type (Ashton) (Chris) in the Find what box and \2 \1 in the Replace with box. Word will find Ashton Chris and replace it with Chris Ashton.
Wildcards for items you want to find and replace
-
When the Use wildcards check box is selected, Word finds only the exact text that you specify. Notice that the Match case and Find whole words only check boxes are unavailable (dimmed) to indicate that these options are automatically turned on. You can't turn off these options.
-
To search for a character that's defined as a wildcard, type a backslash (\) before the character. For example, type \? to find a question mark or \\ to find a backslash character (this is often called an "escape character").
-
You can use parentheses to group the wildcard characters and text and to indicate the order of evaluation. For example, type <(pre)*(ed)> to find "presorted" and "prevented".
To find | Type | Example |
Any single character, including space and punctuation characters | ? | s?t finds sat, set, and s t. |
One of these characters | [ ] | w[io]n finds win and won. |
Any single character in this range | [-] | [r-t]ight finds right, sight, and tight. Ranges must be in ascending order. |
The beginning of a word | < | <(inter) finds interesting and intercept, but not splintered. |
The end of a word | > | (in)> finds in and within, but not interesting. |
Expression | () | Word remembers the results of a search combination to use in a replace operation. |
Any single character except the characters in the range inside the brackets | [!x-z] | t[!a-m]ck finds tock and tuck, but not tack or tick. |
Exactly n occurrences of the previous character or expression | {n} | fe{2}d finds feed but not fed. |
At least n occurrences of the previous character or expression | {n,} | fe{1,}d finds fed and feed. |
From n to m occurrences of the previous character or expression | {n,m} | 10{1,3} finds 10, 100, and 1000. |
One or more occurrences of the previous character or expression | @ | lo@t finds lot and loot. |
Any string of characters, including space and punctuation characters | * | s*d finds sad, started, and significantly altered. |
Use codes to find letters, formatting, fields, or special characters
Codes that work in the Find what box or Replace with box
To find | Type |
Paragraph mark ( ) | ^p (doesn't work in the Find what box when the Use wildcards option is turned on), or ^13 |
Tab character ( ) | ^t or ^9 |
ASCII character | ^ nnn, where nnn is the character code |
ANSI character | ^0 nnn, where 0 is zero and nnn is the character code |
Em dash ( — ) | ^+ |
En dash ( – ) | ^= |
Caret character | ^^ |
Manual line break ( ) | ^l or ^11 |
Column break | ^n or ^14 |
Page or section break | ^12 (when replacing, inserts a page break) |
Manual page break | ^m (also finds or replaces section breaks when the Use wildcards option is turned on) |
Nonbreaking space ( ) | ^s |
Nonbreaking hyphen ( ) | ^~ |
Optional hyphen ( ) | ^- |
Codes that work in the Find what box only (when Use wildcards is turned off)
To find | Type |
Any character | ^? |
Any digit | ^# |
Any letter | ^$ |
Picture or graphic (inline only) | ^g |
Footnote mark | ^f or ^2 |
Endnote mark | ^e |
Field (when field codes are visible) | ^d or ^19 or ^21 |
Comment (when comments are inline) | ^a or ^5 |
Section break | ^b |
White space | ^w (any space or any combination of regular and nonbreaking spaces, and tab characters) |
Code that works in the Replace with box only
To find | Type |
Microsoft Windows Clipboard contents | ^c |
Find and replace text by using regular expressions (Advanced)
About regular expressions
You can automate many find-and-replace tasks by using wildcard characters to build regular expressions, which are combinations of literal text and wildcard characters. The literal text characters indicate text that must exist in the target string of text. The wildcard characters indicate the text that can vary in the target string. For example, you can use regular expressions to find and remove duplicate rows from a large table or to transpose a list of names (change them from "First Last" to "Last, First").
Sample - Try it!
Here's a sample you can try. The steps in this section explain how to use a regular expression that transposes names. Keep in mind that you always use the Find and Replace dialog box to run your regular expressions. Also, remember that if an expression doesn't work as expected, you can always press CTRL+Z to undo your changes, and then try another expression.
-
Start Word and open a new, blank document.
-
Copy the following names into the document.
Josh Barnhill
Doris Hartwig
Tamara Johnston
Daniel Shimshoni
-
On the Home tab, in the Editing group, click Replace to open the Find and Replace dialog box.
-
If you don't see the Use wildcards check box, click More, and then select the check box. If you don't select the check box, Word treats the wildcard characters as text.
-
Type the following characters in the Find what box. Make sure you include the space between the two sets of parentheses, and do not include a space at the end.
(<*>) (<*>)
-
In the Replace with box, type the following characters. Make sure you include the space between the comma and the second slash.
\2, \1
-
Select the names, and then click Replace All. Word transposes the names and separates them with a comma, like so:
Barnhill, Josh
Hartwig, Doris
Johnston, Tamara
Shimshoni, Daniel
The document's content determines most (but not all) of the design of your regular expressions. In the example above, each item (name) contained two words. If the item contained two words and a middle initial, you'd use a different expression.
Let's examine each expression from the inside out:
In the first expression, (<*>) (<*>):
-
The asterisk (*) returns all the text in the word.
-
The less than and greater than symbols (< >) mark the start and end of each word, respectively. They ensure that the search returns a single word.
-
The parentheses and the space between them divide the words into distinct groups: (first word) (second word). The parentheses also indicate the order in which you want search to evaluate each expression.
In other words, the expression says: "Find both words."
In the second expression, \2, \1:
-
The slash (\) works with the numbers to serve as a placeholder. (You can also use the slash to find other wildcard characters. See the next section for more information.)
-
The comma after the first placeholder inserts the correct punctuation between the transposed names.
In other words, the expression says: "Write the second word, add a comma, write the first word."
Using regular expressions and wildcards, with examples
The following examples show you some of the ways that you can use wildcard characters and regular expressions in Word. For a list of the wildcards you can use, see the section Wildcards for items you want to find and replace above.
Example 1: Transpose names with middle initials
Example 3: Add periods to, or remove them from, salutations
Example 4: Find duplicate paragraphs or rows
Example 1: Transpose names with middle initials
This example uses a combination of wildcard characters and character codes to transpose names that contain middle initials. If you're unfamiliar with character codes, see the section Use codes to find letters, formatting, fields, or special characters above.
Keep these facts in mind as you proceed:
-
Whenever you use this expression on names that reside in a table, you must first convert that table to text.
-
If the table contains more than one column, copy the column containing the names to a blank document and convert it to text there.
-
After you transpose the names, convert the text back to a table. You can then delete the original column and replace it with your changed data.
First prepare the sample data:
-
If you haven't already done so, start Word and create a new, blank document.
-
Insert a blank table into the document. Make the table 1 column wide by 4 rows high.
-
Copy these names individually, and paste each one into a blank table cell:
Joshua Quentin Barnhill
Doris X. Hartwig
Tamara Y. Johnston
Daniel ShimshoniYour table should look something like this:
Joshua Quentin Barnhill
Doris X. Hartwig
Tamara Y. Johnston
Daniel Shimshoni
-
Select the table, and on the Table Tools Layout tab, in the Data group, click Convert to Text.
-
Select Paragraph marks as the text separator, and then click OK.
Now transpose the names:
-
On the Home tab, in the Editing group, click Replace to open the Find and Replace dialog box.
-
Select the Use wildcards check box (you may need to click More to see the check box), and then type the following expression in the Find what box:
(*) ([! ]@)^13
Make sure you include a space between the two sets of parentheses and after the exclamation point. If you haven't seen the ^13 character before, we explain what it does in the next section.
-
In the Replace with box, type the following expression:
\2, \1^p
-
Select the list of names, and then click Replace All. Word transposes the names and either middle initials or middle names, like so:
Barnhill, Joshua Quentin
Hartwig, Doris X.
Johnston, Tamara Y.
Shimshoni, Daniel
Convert the changed text back to a table:
-
Select the list of transposed names.
-
On the Insert tab, in the Tables group, click Table, and then click Convert Text to Table.
-
In the Convert Text to Table dialog box, under Separate text at, click Paragraphs, and then click OK.
Transposing with middle initials, piece by piece
Let's look at the individual pieces of the expression to see how they work, starting with the expression in the Find what box.
The entire expression looks for two groups of patterns: a first name with a middle initial (or a middle name) and a last name. The (*) finds all first names. Notice that there's a space after it.
This part of the expression matches the last names:
([! ]@)^13
The exclamation point excludes any character specified in the brackets. In this case, [! ] means "find everything but spaces." Its effect is to trim the space from in front of the last names.
The @ character finds one or more occurrences of the previous character, so it simply ensures that all spaces in front of the last name are removed.
We need to know where the last name ends, so we also use the ^13 character to search for the paragraph mark at the end of each line. However, because we don't plan to reuse the paragraph mark, we surround everything else with parentheses.
You can try this by copying the names to your test document again (make sure you separate them with paragraph marks), and then search using ([! ]@)^13 in the Find what box. Search matches each last name.
Because search starts again at the beginning of the next line, we use the asterisk wildcard character (*) to match everything from there to the beginning of the next last name.
Because we don't plan to reuse the space in front of the last name, we use parentheses to exclude it from the two groups:
(*) ([! ]@)^13
Important: Be careful when using the ^13 character code. Normally, you can use the ^p character code to search for paragraph marks. However, that code does not work in wildcard searches. Instead, you need to use the substitute code ^13. Although the ^p character code does not work in wildcard searches, you should use it in wildcard replace operations because it includes formatting information, and the ^13 character does not. In addition, you cannot assign style information to the ^13 character at all. Misusing the ^13 code in a replace operation can essentially convert your document into a file that you cannot format.
The "replace" expression (\2 \1) does the actual transposition. In the Replace with box, the \2, characters tell search to write the second pattern first and to add a comma after the pattern. The \1^p characters tell search where to write the first pattern and to write a paragraph mark after that pattern.
Example 2: Transposing dates
This example uses regular expressions to convert dates in European format to dates in the U.S. format.
-
Copy and paste the following date into your document: 28th May 2003
-
Open the Find and Replace dialog box, and type the following expression in the Find what box:
([0-9]{1,2})([dhnrst]{2}) (<[ADFJMNOS]*>) ([0-9]{4})
Make sure you insert a space between the following opening and closing parentheses: 2}) (<[ and *>) ([0.
-
Enter the following expression in the Replace with box:
\3 \1, \4
Make sure you insert a space between each set of characters.
-
Click Replace All.
Search replaces 28th May 2003 with May 28, 2003.
Transposing dates, piece by piece
Let's start with the expression in the Find what box. The expression works by breaking dates down into four patterns, denoted by the sets of parentheses. Each pattern contains the components that you find in all dates written in the style that you used in the example. Working from left to right:
-
The number range [0-9] matches the single-digit numbers in the first pattern. Because dates can consist of two numbers, we tell search to return either one-digit or two-digit dates: {1,2}. The result is the first pattern: ([0-9]{1,2}).
Ordinals make up the second pattern. Ordinals consist of "th," "nd," "st," and "rd," so we add those letters to a range [dhnrst]. Because ordinals always consist of two letters, we restrict the letter count to two: ([dhnrst]{2}).
-
Next comes a space, followed by literal and wildcard characters that find month names. All month names begin with these capital letters: ADFJMNOS. We don't know how many characters follow each capital letter, so we follow them with the asterisk (*). We're only interested in the month name itself, so we use greater-than and less-than characters to limit the results to the individual word. The result is the fourth pattern: (<[ADFJMNOS]*>).
-
Finally, we search for the year. We use the same number range, but this time we restrict the count to four letters ([0-9]{4}).
Notice that in the Replace with box we wrote only three of the four address patterns. We omitted the ordinal (the "th") from the date because dates in the U.S. format don't use ordinals. If you want to leave the ordinal in the date, enter \3 \1\2, \4 in the Replace with box. In this case, you enter a space both after the 3 and after the comma, but nowhere else.
At this point, you may ask how to handle dates in which the name of the month isn't spelled out, such as 28/05/03. You search using this expression:
([0-9]{1,2})/([0-9]{1,2})/([0-9]{2})
You replace using this expression:
\3/\1/\2
If the date takes the format of 28/05/2003, you use {4} in the last pattern instead of {2}.
About using list separators in regular expressions
The previous example uses the following argument to find either one-digit or two-digit dates: {1,2}. In this case, a comma separates the two values. However, your regional settings in Windows control the list separator that you use. If your regional settings specify the use of semicolons as list separators, you must use them instead of commas.
To find out which list separator your operating system specifies, do the following:
-
Open Control Panel. (Right-click the Windows Start button, and then click Control Panel in Windows 8 and later. In Windows 7, click the Start button, and then click Control Panel.)
-
Click Clock, Language, and Region.
-
Click Change date, time, or number format, and then click Additional settings.
-
Click the Numbers tab, and then locate the List separator entry.
Example 3: Add periods to, or remove them from, salutations
In some countries, honorific titles (Mr., Mrs., and so on) do not include periods. This example shows you how to add periods to or remove them from honorifics. From this point on, we assume that you know how to use the Find and Replace dialog box.
This expression finds Mr, Ms, Mrs, and Dr without periods:
<([DM][ rs ]{1,2})( )
Notice that the expression uses a second pattern containing a blank space. That space normally would follow the honorific if the period was not there. This expression adds the period:
\1.\2
To do the reverse, search using this expression:
<([DM][ rs ]{1,2}).
And replace using this expression:
\1
Example 4: Find duplicate paragraphs or rows
When you use this expression, you may want to sort the list first to place duplicate rows next to each other. Also, you need to remove all blank paragraph marks. In other words, if you use blank paragraphs to separate blocks of text, like so:
Joshua Quentin Barnhill¶
¶
Joshua Quentin Barnhill¶
¶
Doris X. Hartwig¶
you need to remove those paragraphs, like so:
Joshua Quentin Barnhill¶
Joshua Quentin Barnhill¶
Doris X. Hartwig¶
You can use your favorite method to remove the blank paragraphs, but here's one that finds two consecutive paragraph characters. Search using this expression (the @ character repeats the find-and-replace operation and removes all multiple empty lines):
(^13)\1@
You replace the results with this expression:
^p
Now let's look at ways to replace text. This expression finds any sequence of two consecutive identical paragraphs:
(*^13)\1
This expression also matches longer repetitions of text that end in paragraphs. For example, run the expression against the following list:
Joshua Quentin Barnhill¶
Doris X. Hartwig¶
Joshua Quentin Barnhill¶
Doris X. Hartwig¶
Tamara Y. Johnston¶
Search finds the first four lines and stops only when the overall pattern changes. In contrast, if you run the expression against this list:
Joshua Quentin Barnhill¶
Joshua Quentin Barnhill
Doris X. Hartwig¶
Doris X. Hartwig¶
The expression finds only the first two paragraphs.
To search for a greater number of identical items, add more placeholders. For example, this expression finds three consecutive identical paragraphs:
(*^13)\1\1
You can also use braces to do the same thing. The following examples find two and three identical paragraphs, respectively:
(*^13){2} (*^13){3}
Or, you can find either two or three identical paragraphs:
(*^13){2,3}
You can also find two or more identical paragraphs:
(*^13){2,}
You can replace any of those expressions with the following string:
\1
In addition, you can repeat the find-and-replace operation as needed to replace all the duplicate paragraphs in your document, or you can add the @ wildcard character and have the expression repeat the operation for you:
(*^13)\1@
You also use this method to replace duplicate rows in a table. To do so, first remove any merged cells, and then sort the table to place duplicate cells adjacent to each other. Next, convert your table to text. (On the Table menu, point to Convert, and then click Table to text; when prompted, use the tab delimiter.) After you make your replacements, convert the text back to a table.
More examples
For more examples of how to use regular expressions in Word, see Finding and replacing characters using wildcards on the MVP FAQ site.
See also
Configure or turn off automatic text formatting (AutoFormat)
No comments:
Post a Comment