Lesson 04: How Many

  • Sumo

Quick Takes

To find an entire number or strings of digits:

  • use Repeat > One or More Times
  • expressed as +
  • ex. multiple digits would be \d+

 Combine with . (any character)

  • .+ indicates “any character, one or more times”
  • can be called “stuff,” since it finds anything (except a hard return), one or more times

Use Repeat > Zero or More times (*)

  • Can be though of as “or not.” It may be there, it might not.

If you know exactly how many times a character appears:

  • indicate an exact number within a pair of curly brackets { } ex. {5}
  • indicate a minimum value with a a number followed by a comma ex. {2,}
  • indicate a range with the minimum and maximum separated by a comma ex. {3,5}

Full Text

Up until this point, we’ve basically looked at finding singular characters. Oftentimes when we know exactly what those characters are. But like so many expressions, there are often several ways to write the same thing. Some ways are just better. For instance, just using the wildcard we learned previously that finds any digit: \d. If we do a search on \d on this text here, we can go through and find each individual digit. But what if we wanted to find not the individual digits, but an entire number or string of digits?

And what if we don’t know how many digits that might be? In this sample, we have different amounts of digits in each of our numbers. We can use the hidden menu and look under Repeat…and find One or More Times. So at least one, but who knows how many? And the expression for that is +. So to find multiple digits in a row we can write \d followed by the plus sign.

Here’s an expression that uses One or More Times that you’ll probably use a lot. Period, plus (.+). It’s pretty general. It’s any character, one or more times. That’s going to take in any text in the paragraph with the exception of a hard return. I like to call this expression “stuff.” It’s used when the content of the found text can be just about anything.

Let’s head back to the secret menu, down to Repeat, and look at another option which is Zero or More Times. That’s expressed with an asterisk (*). Now, why call it out if it appears zero times? Well, I like to call this the “or not” expression. Something might be here or not. Let’s actually look at our phone number expression again. If we look at the beginning of this expression, there’s a backslash and an open parenthesis (\() and that’s followed up by the asterisk (*). We have to escape the parenthesis because we want the literal parentheses character and because it has a special meaning and GREP otherwise. But the asterisk says maybe it’s here, maybe it’s not.

In our phone number example, we have a lot of these. Parentheses dashes, close parentheses, spaces, periods, etc. So we’re saying that whether they are there or they aren’t, it still fits the pattern.

Sometimes we have a better idea of what we need to find or our search needs to be a little less inclusive. We might know exactly how many of something we’re actually looking for. Using our phone number example, we know that there are three digits near the start of the expression. We have a couple of these “or not” expressions as well. We’ve got one right in the beginning. But after that, we know that we’re looking for three digits exactly. This number three here in the curly brackets—or curly braces—states that we’re looking for exactly three digits: no more, no less. The number reflects back on whatever character or expression is immediately before it, and it sort of limits our expression.

There are few built-in expressions that come with InDesign and you’ll find them here in the pulldown Query menu. And this middle section between these tiny little gray lines here are actually all GREP expressions. There are a few that are built in, like I said, and one of them that I use a lot is “Multiple Space to Single Space.” This gets rid of all those double spaces that we normally have to go in and manually pull out or do a find/change on by doing “find space-space and replace with space.” This is the better way to do it because it takes into account all the different spaces that might be here and sometimes people put actually more than one space.

This built-in expression uses the number limitation quite well. So this first part in the square brackets sort of chucks at us all the different types of spaces that we might encounter in an InDesign file. But here’s the magic key. In the curly brackets is a two and a comma. That comma says “or more.” So we’re looking for any kind of space… what all of this says here… and we’re looking for it when it occurs two or more times in a row. If there’s only one space, it keeps it there. And if there are two or 20 or 220, it will find that and style the pattern. And in this case, we’re telling it replace it with one single space.

We can also create a range of characters to find. Maybe we want to find words in a spelling list that are four to six characters long. And since these are all in lower case, we’re just going to go ahead and look for lowercase characters. So the first thing I’ll do is enter the wildcard for lowercase: \l. And then I’m going to go ahead and create a pair of curly brackets—and I’ll use my back arrow key to get inside those brackets—and then I’ll start with the minimum value. We said we want words that are at least four or a string of four lowercase characters at least, and then I’m going to put a comma. And in this case, the comma indicates a range. So I’m going to put the minimum then comma and the maximum, which is six. So that’s any lower case character four, five, or six times in a row. I’m going to select that frame and make sure I’m only working on that story and say, “find next.” And it skips to the first word that is 4 characters long. And as I hit find next, it goes through each of those words.

Notice that this simple expression works because each word is its own paragraph in this case. Also note that no word’s longer than our maximum. I tailored the text to fit the expression to make the example as simple as possible. If the text was more complex, we would need to add even more to our expressions. We’ll cover more involved expressions in the other lessons in this course as well as in future courses.

In the next lesson, we’ll create expressions for text in specific locations or based on its location relative to other text.