Tuesday, August 30, 2005

Replace find-and-replace with regular expressions in Microsoft Word

A regular expression is a combination of literal and wildcard characters that you use to find and replace patterns of text.

Microsoft Office Word provides a set of wildcard characters that you can use to build regular expressions, you can use regular expressions to find text that matches a given pattern and then replace those matches with new text.

Some example of using RE in MS Word:

w[io]n finds “win” and “won” but not “worn,” because the “r” is not specified.

[r-t]ight finds “right” and “sight.” The ranges you specify must be in ascending order. In other words, you can specify [a-m], but not [m-a].

fe{1,}d finds “fed” and “feed.” (At least n occurrences of the previous character or expression)

s?t finds “sat” and “set.” This character also finds the chosen combination of characters within a word. For example, it could locate “set” within “inset.”

Here’s a practical example of using regular expressions for transposing dates:

Search replaces 28th May, 2003 with May 28, 2003.

1. Copy and paste the following date into your document: 28th May 2003
2. Open the Find and Replace dialog box, and enter the following expression in the Find what box: ([0-9]{1,2})([dhnrst]{2}) () ([0-9]{4}) Make sure you insert a space between the following opening and closing parentheses: 2}) () ([0.
3. Enter the following expression in the Replace with box (make sure you insert a space between each set of characters), and then click Replace All: \3 \1, \4

More Resources on using Regular Expressions in Microsoft Office Word 97, 2000, and 2002:

 Putting regular expressions to work in Word
 Microsoft Office Assistance: Add power to Word searches with regular expressions
 Using regular expressions on the MVP FAQ site - Finding and replacing characters using wildcards.
 Jeffrey Friedl’s book Mastering Regular Expressions

No comments:

Post a Comment