bottom
Great WordTips!
         
Your e-mail address is safe!
Close Note

Tips.Net > WordTips Home > Editing > Find and Replace > Ordering Search and Replace

Ordering Search and Replace

Summary: Using the wildcard capabilities of Word’s Find and Replace feature, you can do some fairly complex operations. This tip describes how you can use parentheses in what you are search for in order to affect what it is replaced with. (This tip works with Microsoft Word 97, Word 2000, Word 2002, and Word 2003.)

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, 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.

Tip #1663 applies to Microsoft Word versions: 97 | 2000 | 2002 | 2003


Take Control! Experienced users know that styles can make the difference between a plain document and a masterful one. This is the real power behind Word, and the key to that power can be found in WordTips: Styles and Templates.

Helpful Links

Ask a Word Question
Make a Comment

Tips.Net Home
Vital News Home

WordTips FAQ
WordTips Premium

Learn Access Now

Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips

Advertise on the
WordTips Site

 

Great Info!

Get tips like this every week in WordTips, a free productivity newsletter. Enter your e-mail address and click "Subscribe."
     
(Your e-mail address will never be shared with anyone, ever.)