Subscribe through Feedburner

Practical tips to help you use Macs, iPhones and iPods

Subscribe by Email · Subscribe to the MacTips feed · Follow MacTips on Twitter. · Follow us on YouTube. · Join MacTips on Facebook.

Pattern Replace (1)

Wed 01 November 2006

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 first of 3 articles.

Pattern Replace (1)
Mac Tip #267/01-Nov-2006

Imagine you have a list of names like this:

  • Janeway, Kathryn
  • Summers, Buffy
  • Carter, Samantha

Here we have a last name, a comma and space, followed by a first name. Now suppose you’d like those names to be reversed like this:

  • Kathryn Janeway
  • Buffy Summers
  • Samantha Carter

Pattern search

You can either do a lot of copy and pasting, or you can use a clever find and replace routine, employing a pattern search called grep: check each line for a first group of letters followed by a comma and space, and then a second group of letters. Replace that whole thing with the second group of letters, a space and the first group.

Suitable software

For the following instructions I use the free Tex-Edit Plus text editor.

Some other software can use grep too, but it may not follow precisely the instructions below.

The regular expression (grep)

Paste the first list of names into an empty Tex-Edit Plus document, then use Command F to call up the Find and Replace dialog box.

In the Find text box put exactly this (I explain it below). Note the comma and space in the middle:

([[:alpha:]]+), ([[:alpha:]]+)

In the Replace text box put this (note the space):

^2 ^1

Check the box labelled Regular expression (grep), then click the Replace All button. The names should now be reversed.

An explanation of terms

The round brackets () create groupings — in this case groups of letters. In the replacement, ^2 refers to the second grouping, and ^1 refers to the first grouping.

The square brackets [] contain the possibilities of what we’re looking for, in this case any alphabetic character, or letter. We could write out all the letters of the alphabet inside the square brackets, but I’ve used a shorthand above to stand in for all the letters: another set of square brackets, a pair of colons and the word alpha: [:alpha:].

Since that sequence would find only any one letter; the + tells the program to find more than one letter.

So, in English, we could say: work line by line to look for a group consisting of one or more letters, followed by a comma and a space, followed by another group of letters. Replace that pattern with the second group, a space, and the first group.

It looks complicated, and it is, a bit. But it is the start of something immensely powerful.

Help

When you download and install Tex-Edit Plus it includes a file called Grep? that contains useful information about what regular expressions are and how to construct them for use within Tex-Edit Plus.

Next week: Do a similar task in Microsoft Word (2004).

Related Posts

  1. Pattern Replace (3) Regular Expressions (grep) let you do find and replace by...
  2. Pattern Replace (2) Regular Expressions (grep) let you do find and replace by...
  3. Count with Find and Replace Find and Replace is a quick way to do some...
  4. Find and Replace Find and Replace makes it quick and easy to replace...
  5. Replace in technicolour Tex-Edit Plus can find and replace styled text. ...

Hey. Welcome back. Sign up for free MacTips updates via RSS or email, follow MacTips on Twitter and check our YouTube channel. Check for more Tips on this topic. We love your Comments too.

{ 1 comment… read it below or add one }

1 Norbert C Ballauer Thu 02 November 2006 at 07:31:28

Ms Jordan, now that I am back I have wanted to vote about your tips (upper right hand side) but I am unable to find where to do it or how, when ever you have a free minute or two I would appreciate very much knowing how as I look forward to each Tuesday for your tips.

Norbert P.S. Thank you for taking the time and trouble in putting this information.

Reply

Leave a Comment