Pattern Replace (2)

Regular Expressions (grep) let you do find and replace by pattern, eg swapping the order of words in a list. It’s a powerful technique and not as hard as you may think. Here’s the second of 3 articles.

 

Pattern Replace (2)
Mac Tip #268/08-Nov-2006

In last week’s Tip I showed how to switch a pair of names using grep in Tex-Edit Plus. Please read last week’s Tip again to refresh your memory of what’s going on. We used a regular expression to replace a reversed name like Janeway, Kathryn with this form: Kathryn Janeway.

MS Word and regular expressions

Expand the Find and Replace options in Word.

Expand the Find and Replace options in Word.

Microsoft Word is a popular wordprocessing program, and you’ve probably used its Find and Replace feature to do a simple task such as fixing a consistent typo. You may not be aware that it also supports some Regular Expressions (grep) too.

To be brutally honest, I don’t care much for Word myself and it took literally several hours for me to work out how to do last week’s task in Word 2004.

I consulted the Help files, I Googled, I tried endless variations to finally come up with the solution below. There may be other, equally valid solutions. If you know of any (that you’ve tested) that do this task in Word, please leave a comment on this post.

Frankly, I’d copy such text out of Word, paste it into Tex-Edit Plus, do the transformation there and then return the final text to Word. The following may get you started though on a long and happy association with Word’s advanced Find and Replace features.

Expand Word’s options

Paste the list of names from last week’s Tip into an otherwise empty Word document and press Command Shift H to call up the Find and Replace dialog box.

If there is an upward pointing triangle at the left end of a row containing several replace buttons at the bottom of the window, then click the triangle to expand Word’s Search options.

The window becomes larger and more checkboxes and buttons become visible.

The Special pop-up

The Special pop-up at the bottom of the expanded window allows you to insert various patterns into the Find what text box. For example, click in the Find what text box and choose Expression from the Special pop-up. Word inserts a pair of round brackets in the text box. You can fill in what you need to find between the brackets. Note that the exact choices available depend on whether you have “use wildcards” checked in the Search options.

As you can see, if you have Word, there are many Special items you can use, such as tab characters or page breaks.

The formula

The formula I finally conjured, after much trial and error finds an expression where the beginning of a word (<) is followed by one or more (@) of any character (?) and then the end of a word (>). This expression is followed by a comma and a space and then another iteration of the same expression.

It uses wildcards which can stand for any character and, just as last week’s Tip did, replaces what it finds with the second expression followed by a space and then the first expression. It looks like this:

Find what: (<?@>), (<?@>)

Options: Use wildcards

Replace with: \2 \1

Help

Some limited help on Special items is available from Word’s Help menu.

Swings and roundabouts

The Regular Expression I used in Word is different from the one I used in Tex-Edit Plus, although the end result is very similar.

In Word I looked for any character at all — a name such as O'Leary that includes punctuation would not cause problems.

The Tex-Edit Plus version looked for letters only — the apostrophe in O'Leary would not be recognised and would cause that particular name to be mangled. I could construct the regular expression in Tex-Edit Plus to get around that problem, but was more interested in keeping it simple for the moment.

Next week: Do a similar task in TextWrangler.

Thanks for reading. Remember to tell your friends about MacTips. Subscribe for regular Tips.

Clip to Evernote

Tell us what you think.
Note: your comment is not published straight away. I check and approve all comments, otherwise the spammers get a free rein. I usually check comments within 24 hours.

Add your Comment

Take Control ebooks (affiliate link) are superb.
I do some of my best learning from them. Buy through my link so I can make more and better Tips.

Take Control of Using Lion.   Buy Take Control of iCloud.  Macworld iPhone 4S Superguide   Take Control of iTunes 10: The FAQ, Second Edition