What is GREP?
A good example of GREP in action is creating a pattern to recognize all phone numbers in a document and then standardize their formatting:
GREP in InDesign lives in two places:
Within a paragraph style:
GREP. What is it? What does it stand for? What does it do? Well, first of all, GREP is an acronym, and we’re not going to really worry about what the acronym stands for, except for the R and the E, which stands for “Regular Expressions.” This is a term you may hear used elsewhere. At its very basic, GREP in InDesign is finding patterns of text and then it allows us to do something to that text that we find. We can find a pattern of text in our document and then style it in such a way based on the fact that it fits that pattern.
But let’s actually look at GREP in action. That way we can actually see what GREP does for us. First of all, GREP lives in two places in InDesign: in the Find/Change dialog box and also in paragraph styles. There are different reasons and pros and cons to each and we’ll talk about those in just a little while.
So on this page I have a lot of numbers. If you live in North America or you’ve ever called a phone number in North America, you might recognize these as phone numbers. What you also might recognize is there’s a lot of ways to actually format these North American phone numbers. Sometimes we have parentheses or dashes or dots or spaces or just ten digits run altogether. But generally speaking, in a document we’d probably want them to be all formatted the same. GREP can do this for us. It will find the pattern that we set up and actually format it in such a way that it’s standardized across our document. Let’s actually look at that in action.
I’m going to open up the Find/Change dialog box by doing Command- or Ctrl-F, and then I’m going to just go ahead and select this story quickly. Then I’m going to choose an expression that I’ve already set up here. And we’re not going to look at anything, why this is, what it is. We’re just going to see how it works. So I’m going to choose this query that’s here and I’m going to tell it to go ahead and Change All. It says it made eight replacement’s. Let’s see those replacements. There they are. So with the click of that button, it took all those phone numbers, formatted in different ways, and it standardized them to how I want them to look. I also changed it to orange, just so we could see that it had been changed. GREP was able to see this pattern even though the phone numbers looked very different and actually see it as a pattern and then format it in such a way that it’s standardized.
The other place that GREP lives in InDesign is within paragraph styles. On this page, I have a bunch of text and I have a company name that appears in a couple places: The Cybus Corporation. But sometimes it’s just referred to as Cybus. In all cases in this text, I’d like anytime the company name appears, whether it’s Cybus or The Cybus Corporation, I would like it to appear in a company color. I can do this by building a GREP expression into a paragraph style. So I’m just going to change it from the paragraph style that’s already applied, which is the “Corporate Body Text” to “Corporate Body Text CYBUS,” which has a GREP expression built in. When I change that, I can see that it changed The Cybus Corporation and also just Cybus into the company colors. And again, that’s because I’ve built a GREP expression into this paragraph style.
Now there are pluses and minuses to both of these. For instance, in the Find/Change dialog box where we had the phone numbers, I was able to add or subtract, and I could even rearrange the text that it finds with the pattern. So it identifies the pattern, and then I can do all sorts of things to it: remove, add, rearrange. For instance, we removed some parentheses, we removed spaces, I removed dashes, and I put in these dots wherever I needed them. And that’s because it was Find/Change. That’s a big plus. One of the downsides to that is that if I were to add new text to this, it won’t automatically format it. I’ll have to run that find/change again anytime my text changes.
When building it as part of a paragraph style, however, anytime I add new text it will actually change. Let’s do that. And I’ll just type “at Cybus” and as soon as I type in Cybus, it fits the pattern and it went ahead and applied that styling to it. So that’s a huge plus. One of the drawbacks to using the paragraph style is that we’re limited to what can be added via character style, which is how we’re doing the styling. For the most part, we can’t add or subtract or do anything like that. We basically can find text and apply a character style to all the text that it finds.
In the next lesson, we’re going to look at how to use the Find/Change dialog box to build a better expression and to reuse those expressions as we build them.