Wildcards in the Real World

I hope you've enjoyed the wildcard "tutorial" articles in Editorium Update over the past few weeks. If you haven't read them, I'd recommend that you do so in order to understand this week's article. You can review the whole series here, starting with the March 20, 2001, issue:

http://www.editorium.com/euindex.htm/

This week I thought you might be interested in seeing some of the wildcard combinations I've used recently in an actual editing project. Maybe you'll find them useful too.

EXAMPLE 1

The manuscript I've been working on has lots of parenthetical references like this:

(Thoreau, Walden, p 10.)

You'll notice that there's no period after the p. To fix these references, I used the following string in Microsoft Word's "Find What" box in the Replace dialog (Edit > Replace), with "Use Wild Cards" (or "Use Pattern Matching") turned on:

p ([0-9]@.))

That's an odd-looking thing with its double parentheses, but its meaning becomes clear when you consider that the first closing parenthesis represents the closing parenthesis of the reference. The backslash in front of it tells Word to treat it as a character rather than the end of a group "expression." So the whole string says this:

1. Find a p followed by a space.

2. Find, as a group, one or more digits followed by a period followed by a closing parenthesis.

I put this in the "Replace With" box:

p. 1

And that string says this:

1. Replace the p followed by a space with p followed by a period and a space.

2. Replace the rest of the "Find What" string (the group in parentheses) with itself.

When I was finished finding and replacing, the references looked like this:

(Thoreau, Walden, p. 10.)

EXAMPLE 2

Here's another example from the manuscript I've been working on:

(Genesis 8:26)

You'll notice that there's no period before the closing parenthesis. Wanting to fix these, I put this string in the "Find What" box:

([0-9]@:[0-9]@))

It says:

1. Find, as a group, any number of digits followed by a colon followed by any number of digits.

2. Find a closing parenthesis character.

I put this in the "Replace With" box:

1.)

And that string says:

1. Replace the group with itself.

2. Replace the closing parenthesis with a period and a closing parenthesis.

When I was finished finding and replacing, the references looked like this:

(Genesis 8:26.)

"Why," you may be wondering, "did you have to use wildcards? Why didn't you just find a closing parenthesis and replace it with a closing parenthesis and a period, like this:

Find What:

)

Replace With:

.)

I couldn't do that because the manuscript had other parenthetical items (like this one) that didn't need a period. Using wildcards makes it possible to find exactly the items you want and ignore those you don't.

EXAMPLE 3

The manuscript had Bible references that looked like this:

II Corinthians

II John

II Kings

I wanted them to look like this:

2 Corinthians

2 John

2 Kings

I put this in the "Find What" box:

II ([A-Z])

The string says:

1. Find I followed by I followed by a space.

2. Find any capital letter.

And I put this in the "Replace With" box:

2 1

That string says:

1. Replace the II with a 2.

2. Replace the capital letter with itself.

Worked like a charm.

"Why," you ask, "didn't you just replace II with 2 throughout the manuscript rather than use wildcards?" Well, I could have. But I was also thinking about other entries like these:

I Corinthians

I John

I Kings

Obviously, I couldn't just replace I with 1 throughout the manuscript, so I used this string in the "Find What" box:

I ([A-Z])

And I used this string in the "Replace With" box:

1 1

And that took care of the problem.

I hope you're beginning to see how powerful wildcards can be and how much time they can save while you're editing a manuscript. Using wildcards, you can quickly fix repetitive problems that would take hours to correct by hand. I highly encourage you to try them, but I also urge you to back up your documents and experiment on some junk text before using wildcards in the "real world." Also, try finding and replacing items individually before replacing all of them globally. Then you'll know that the wildcards you're using actually do what you need to have done.

Using the "Find What Expression" Wildcard

For the past few weeks we've been talking about using wildcards to find and replace text in Microsoft Word. Last week I introduced the "Find What Expression" wildcard (n) and promised to show you how to use it to move things around.

Let's say you've got a list of authors, like this:

Emily Dickinson

Ezra Pound

Willa Cather

Ernest Hemingway

and you need to put last names first, like this:

Dickinson, Emily

Pound, Ezra

Cather, Willa

Hemingway, Ernest

You can use the "Find What Expression" wildcard to do this in a snap.

Start the Replace dialog (Edit > Replace) and put a check in the "Use wildcards" or "Use Pattern Matching" box (you may need to click the "More" button before this is available). Then, in the "Find What" box, enter this:

^013([A-z]@) ([A-z]@)^013

If you've been reading Editorium Update, you'll probably understand these codes and wildcards:

^013 represents a paragraph mark.

[A-z] represents any single alphabetic character, from uppercase A to lowercase z.

@ represents any additional occurrences of the previous character--in this case, any single alphabetic character, from uppercase A to lowercase z.

() groups [A-z]@ together as an "expression" representing an author's first name. (This grouping is the key to using the "Find What Expression" wildcard in the "Replace With" box.)

The space after the first ([A-z]@) expression represents the space between first name and last name.

The next ([A-z]@) group represents the author's last name.

The final ^013 represents the paragraph mark after the name.

Now, in the "Replace With" box, enter this:

^p2, 1^p

The ^p codes represent paragraph marks. "Wait a minute," you say. "You just used ^013 for a paragraph mark. Why the change?"

Excellent question. The answer has two parts:

1. If we could use ^p in the "Find What" box, we would. But since Word won't let us do that when using wildcards (it displays an error message), we have to resort to the ANSI code, ^013, instead. You can learn more about this here:

http://www.topica.com/lists/editorium/read/message.html?mid=1703875043

2. If we use ^p in the "Replace With" box, Word retains the formatting stored in the paragraph mark (a good thing). If we use ^013, Word loses the formatting for the paragraph (a bad thing). In a list of author names, this probably doesn't matter, but you'll need to know this when finding and replacing with codes in more complicated settings.

Continuing with our example, ^p2, 1^p:

2 is the "Find What Expression" wildcard for our *second* expression (hence the 2) in the "Find What" box--in other words, it represents the last name of an author in our list.

The comma follows this wildcard because we want a comma to follow the author's last name.

A space follows the comma because we don't want the last and first names mashed together, like this: "Pound,Ezra."

1 is the "Find What Expression" wildcard for our *first* expression (hence the 1) in the "Find What" box--in other words, it represents the first name of an author in our list.

Now click the "Replace All" button. The authors' names will be transposed:

Dickinson, Emily

Pound, Ezra

Cather, Willa

Hemingway, Ernest

You've always wondered how to do that, right? But now you're wondering about middle initials. And middle names. And Ph.D.s.

All of those make things more complicated. But here, in a nutshell, are the Find and Replace strings you'll need for some common name patterns (first last, first middle last, first initial last, and so on). First comes the name pattern, then the Find string, and finally the Replace string, like this:

NAME PATTERN

FIND WHAT

REPLACE WITH

William Shakespeare

^013([A-z]@) ([A-z]@)^013

^p2, 1^p

Alfred North Whitehead

^013([A-z]@) ([A-z]@) ([A-z]@)^013

^p3, 1 2^p

Philip K. Dick

^013([A-z]@) ([A-Z].) ([A-z]@)^013

^p3, 1 2^p

L. Frank Baum

^013([A-Z].) ([A-z]@) ([A-z]@)^013

^p3, 1 2^p

G. B. Harrison, Ph.D.

^013([A-z].) ([A-Z].) ([A-z]@,) (*)^013

^p3 1 2, 4^p

J.R.R. Tolkien

^013([A-Z].)([A-Z].)([A-Z].) ([A-z]@)^013

^p4, 123^p

That list doesn't show every pattern you'll encounter, but it should provide enough examples so you'll understand how to create new patterns on your own--which is the whole point of this article. Once you've created all of the patterns you need, you could record all of that finding and replacing in a single macro that you could run whenever you need to transpose names in a list.

_________________________________________

READERS WRITE

After reading last week's newsletter, Mary L. Tod (mtod@earthlink.net) wrote:

In your Editorium Update for today, is it necessary to enclose the space in parentheses? Since it isn't being replaced by itself, can't the expression in the Find box be reduced to

(^013[0-9]@.)

(with just the space entered after the first expression)?

Mary is absolutely right about this. I put the space in parentheses because I wanted to briefly introduce the idea that you could have more than one "Find What Expression" wildcard--in this case, 2. For that to work, the space has to be in parentheses so it's recognized as an expression. But I didn't actually *use* the 2 in the example, so a simple space would have worked just fine.

Mary continued:

In a related question, does the @ symbol in the wildcard field also allow for no repeats of the previous character? Otherwise, it would start the list at 10, wouldn't it?

2. followed by a number ([0-9])

3. followed by one or more numbers (@)

Again, this is right on the mark. The @ really means "followed by one or more numbers *if there are any.*" A more technical way to put it is "followed by *zero* or more numbers."

Thanks to Mary for her astute comments.

Wildcard Grouping

For the past few weeks we've been talking about using wildcards to find and replace text in Microsoft Word. This week we'll discuss wildcard grouping, which is simply a way of telling Word that you want certain wildcards to be used together as a unit.

Continuing with our example from last week, let's say that you're editing a document with lots of numbered lists, like this:

1. Lorem ipsum dolor sit amet.

2. Ut wisi enim ad minim veniam.

3. Duis autem vel eum iriure dolor.

Now let's say that you want to replace the space after each number and period with a tab. After calling up the Replace dialog (Edit > Replace) and putting a check in the "Use wildcards" or "Use Pattern Matching" box, you could enter the following string of characters into the "Find What" box:

^013[0-9]@.

(You can't see it, but there's a space on the end of that string, and it needs to be included.) As you probably recall from the past few weeks, this tells Microsoft Word to do the following:

1. Find a paragraph mark (^013)

2. followed by a number ([0-9])

3. followed by one or more numbers (@)

4. followed by a period (.)

5. followed by a space ( ).

But that still won't let us replace that space with a tab. Why? Because there's no way to replace the space independently of the rest of the string--whatever the string finds *includes* the space.

So let's try this:

(^013[0-9]@.)( )

Notice that we've grouped the wildcards and other characters together with parentheses. (In case you can't tell, that's our uncooperative space between the last two parentheses.) Such groups, for reasons known only to the mathematically minded, are called "expressions," and in this case there are two of them:

1. (^013[0-9]@.)

2. ( )

Grouping things together like this makes it possible to refer to each group independently in the "Replace With" box--a wonderful thing! So in the "Replace With" box, we'll enter this string:

1^t

That "1" is an example of the little-known "Find What Expression" wildcard, which lives deep in the wilds of Redmond, Washington, and only comes out at night. It's a backslash followed by the number one, and it tells Word to replace whatever is found by the first expression--

(^013[0-9]@.)

--with whatever the first expression finds. (Yes, you read that correctly.) In other words, Word replaces whatever the first expression finds with *itself.* That seems strange, but it means we can treat the second expression--

( )

--as an independent unit, which is exactly what we need to do. (By the way, "Find What Expression" wildcards are the only wildcards that can be used in the "Replace With" box. They are simply a backslash followed by a number.)

The ^t, of course, is the code for a tab, as explained in the November 14, 2000, issue of Editorium Update:

http://www.topica.com/lists/editorium/read/message.html?mid=1703968584

You'll notice that we haven't included a "2" code, which would replace something with whatever is found by our *second* expression, the space in the parentheses. Since we haven't included that code, the space will be replaced by nothing--in other words, it will be *deleted* during the Find and Replace. So the relationship between the wildcards in the "Find What" string and the "Replace With" string is something like this:

FIND WHAT: REPLACE WITH:

(^013[0-9]@.) > 1 (followed by a tab: ^t)

( ) > [nothing]

Now let's try using them:

1. Start the Replace dialog (Edit > Replace).

2. Put a check in the "Use wildcards" or "Use Pattern Matching" box (you may need to click the "More" button before this is available).

3. In the "Find What" box, enter this:

(^013[0-9]@.)( )

4. In the "Replace With" box, enter this:

1^t

5. Click the "Replace All" button.

Presto! All of the spaces after your numbers will be replaced with tabs, and your list will now look like this:

1.Lorem ipsum dolor sit amet.

2.Ut wisi enim ad minim veniam.

3.Duis autem vel eum iriure dolor.

To me, this is like magic, and it comes in handy more often than you might think. I hope you'll find it useful! In the future, I'll try to provide other examples that you can apply in your day-to-day work. Next week I'll show you how to use "Find What Expression" codes to move things around.

_________________________________________

READERS WRITE

After reading our past few newsletters on wildcard searching, a subscriber wrote, "Use Pattern Matching does not appear to be an option in my Word program."

I apologize for not explaining this. In Microsoft Word 6 and 95, "Use Pattern Matching" is an option in the Find and Replace dialogs, and selecting this option tells Word that you're going to use wildcards. In Word 97 and later, this option is simply called "Use Wildcards." To see this option, you may need to click the "More" button in the Find and Replace dialogs.

Wildcard Ranges

Last week we discussed using wildcard combinations to find text in a Microsoft Word document. You can read last week's newsletter here:

http://www.topica.com/lists/editorium/read/message.html?mid=1706069286

This week we'll talk about wildcard ranges, which you'll probably use a lot.

Wildcard ranges are fairly simple. You just use the [-] wildcard to tell Microsoft Word what to find. Let's continue with our example from last week:

b?[td]

As you probably recall, this tells Word to find the letter b followed by any single character followed by either t or d. In other words, it will find "bet," "but," "bit," "bat," "bed," "bud," "bid," "bad," and so on.

But what if we wanted to find "bat," "bad," "bet," and "bed" but NOT "bit," "bid," "bud," and "but"? After bringing up the Find dialog (Edit > Find) and turning on "Use Pattern Matching" (you may need to click the "More" button before this is available), we could use this wildcard combination in the "Find What" box:

b[a-e][td]

This tells Word to find the letter b followed by any letter from a to e (in other words, a, b, c, d, or e) followed by t or d. (The range *must* be in ascending order--in other words, from a "lower" letter [such as a] to a "higher" letter [such as z].)

Here's another way to approach this:

b[!f-z][td]

Notice the exclamation mark at the front of the "range" wildcard. The exclamation mark tells Word to find every character *except* those specified--in this case, the letters f through z. This wildcard combination, too, will find "bat," "bad," "bet," and "bed" but not "bit," "bid," "bud," and "but."

Here's a range that I use all the time:

[0-9]

This little beauty finds any occurrence of a digit. What's that good for? Let's say you're editing a document with lots of numbered lists, like this:

1. Lorem ipsum dolor sit amet.

2 Ut wisi enim ad minim veniam.

3. Duis autem vel eum iriure dolor.

Did you notice that the number 2 has no period? Good! You must have "the eye." But if you have several long lists, you might want to let Word find these problem numbers for you. To do so, try this wildcard string:

^013[0-9]@[!.]

Pretty cryptic. But if you've been reading Editorium Update, you can probably figure this out:

^013 is the numeric code for a carriage return.

[0-9] represents any digit.

@ tells Word to find one or more occurrences of the previous expression (in this case, any digit). This is necessary in case you have lists with two-digit (or longer) numbers.

[!.] tells Word to find any character *except* a period.

Piece of cake.

Here are two other wildcard ranges you might find useful:

[a-z] represents any occurrence of a lowercase letter.

[A-Z] represents any occurrence of an uppercase letter.

Remember, too, that you can use the [] wildcard (without a hyphen) to specify a whole group of characters *without* using a range. For example, this wildcard will find various kinds of punctuation:

[.,;:?!]

You may be wondering about the backslash () in front of the question and exclamation marks. The backslash tells Word to treat the following character *as* a character and not as a wildcard. (Remember, ? is the wildcard for a single character, and ! is the wildcard for "except.")

Don't be afraid to try all of these wildcard combinations and ranges for yourself (on some junk text, of course). As you experiment, you'll better understand what works and what doesn't. Then, when the need to use wildcards arises (which it will), you'll be ready.

Next week, we'll look at expression grouping and the little-known "Replace With" wildcard.

You can learn more about using numeric codes (such as that ^013 representing the carriage return) here:

http://www.topica.com/lists/editorium/read/message.html?mid=1704081834

And you can learn more about using junk text (such as "Lorem ipsum dolor sit amet") here:

http://www.topica.com/lists/editorium/read/message.html?mid=1705763701

_________________________________________

READERS WRITE

Our last newsletter used misspellings of the name "Gandhi" as an example, noting that it would be possible to use the wildcard string G[andh][andh][andh][andh]I to find the misspellings "Ghandi," "Gahndi," and "Ganhdi" all in one pass. Subscriber Glade Lyon (my dad!) wrote:

"It seems to me that your string should be G[andh][hand][ahnd][anhd]i."

Thinking that other readers might see this the same way, I'm including my response here:

I see what you're thinking--that each set of bracketed letters is an alternative spelling. No, *each set* of bracketed letters represents *one* letter in the word. [andh] will find either an "a," an "n," a "d," or an "h," whichever it comes to first. So, G[andh] will find:

Ga

Gn

Gd

or Gh

G[andh][andh] will find:

Gaa

Gan

Gad

Gah

Gna

Gnn

Gnd

Gnh

Gda

Gdn

Gdd

Gdh

Gha

Ghn

Ghd

or Ghh

And so on. So the point of using G[andh][andh][andh][andh]i is to find every possible four-letter combination of a, n, d, and h. That way, no matter *how* many ways our author has misspelled "Gandhi," we'll catch them all.

In other words, the order of the characters inside the brackets doesn't matter. The strings you suggested--

[andh]

[hand]

[ahnd]

and [anhd]

--are all functionally identical. Each one tells Word to find either an "a," an "n," a "d," or an "h."