Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Ordering Search and Replace.

Ordering Search and Replace

Written by Allen Wyatt (last updated October 17, 2020)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


Pattern matching provides you with some very powerful capabilities for searching and replacing text. Word allows you to use parentheses within your search specification to increase the power of your statements even more. The parentheses allow you to specify groupings for your searches or to specify the order in which the search specification should be evaluated.

The grouping issue is the most common and powerful use of the parentheses. As an example, let's say that you had a document that consisted of a part number followed by a tab followed by a description. The part number begins with PN- followed by a three-character code that has A through E as the first character followed by a value between 00 and 99. For instance, a part number could be PN-B34 or PN-A12. The description could be any length but is always followed by a paragraph mark.

If you wanted to change your document so that you had the part description followed by the part number within parentheses, you could do so with pattern matching. For your search specification (Find What) you would use (PN\-[A-E][0-9]{2})(^t)(*)(^13). This may look odd, but it matches the way your document now is put together. Notice the use of the parentheses to denote the three separate parts: the part number, the tabs, and the description.

For the replacement specification (Replace With), you would use \3 (\1)^13. This means "take the third grouping from the search specification (the description), follow it by a space, a left parenthesis, the first grouping from the search specification (the part number), and finish off with a paragraph mark.

As another example, let's assume that you have a document with quite a few dates that use the format 06/11/56 (June 11, 1956) and that you want to make sure that all the dates use four-digit years (1956 as opposed to 56). You can do so by searching for ([0-3][0-9]/[01][0-9]/)([0-2][0-9]) and replacing it with \119\2. In the example, the \1 is 06/11/ in the string that is found, and this is transferred to the replacement string as it is. This is then followed by 19 (so it is now 06/11/19) and then the last two digits of what was found, so the result is 06/11/1956.

Remember that to take advantage of the pattern matching capabilities of Word, you just need to make sure the Use Wildcards check box is selected on either the Find or Replace tabs of the Find and Replace dialog box.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (9595) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word here: Ordering Search and Replace.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Converting Many DOC Files to DOCX

Need to convert a bunch of old DOC files to the newer DOCX format? Word doesn't provide the capability to convert a bunch ...

Discover More

Typing Pronunciations of Words

Take a look in a dictionary at the way that words are phonetically spelled. Those special characters used to type those ...

Discover More

Centering Information in Table Cells

One of the most common ways to format information in a table is to apply some sort of alignment to the contents of table ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (ribbon)

Replacing Text and Capitalizing a Letter in One Step

The Find and Replace capabilities of Word are very powerful. You can even use them to do some complex and specific ...

Discover More

Matching At the Beginning or End of a Word

The pattern matching capabilities of Word's search engine are quite powerful. You can tailor your search pattern so that ...

Discover More

Replacing Two Tabs with a Space in Limited Situations

The Find and Replace feature of Word is very powerful, allowing you to finely target exactly what you want to search. ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 5 - 3?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.