Join David Blatner for an in-depth discussion in this video 1. GREP basics: Using codes to find text, part of InDesign: 10 Things to Know About GREP.
- View Offline
So GREP is all about finding patterns of text and you're already familiar with patterns of text. Like what's a bunch of letters all grouped together? Well, that's a word, right? Or what's a bunch of words all grouped together. Well, that's a sentence or maybe a paragraph. But you can't say to InDesign, find me a bunch of words all grouped together. Now you need to type in codes, GREP codes. That's what InDesign is expecting. So the good news is that those codes are either really simple or InDesign will actually type them for you. Now because we're searching for text the best place to go and do that is the Find/Change dialog box. I'll open the Find/Change dialog box by pressing Command+F or Ctrl+F on Windows and we'll see that there are actually four different dialog boxes in one here: Text, GREP, Glyph and Object. We're going to be focusing not on text, which is where most people do their work but instead on GREP.
So I'll click on GREP or you can press Command+2 or Ctrl+2 on Windows to jump to that second tab and I'm going to type my code into the Find What field. So I'm going to search for some kinds of chocolate. Let's say I'll search for a Milk chocolate and I'll click Find and it will jump right to the first instance of Milk chocolate. Let's see where is that, there it is, right in the text here. And I zoom in to 200% here. I'll click on the document and then go into 200% with a Command+2 or Ctrl+2 on Windows, and there is the text that I found Milk chocolate.
But in this case I want to find Milk chocolate or maybe Dark chocolate, one or the other. So let's go ahead and type in some codes for that. I'm going to type Dark here at the beginning and I want to tell InDesign to search for either Dark or Milk chocolate. So I'm going to add a vertical bar. On the US keyboard that's Shift+\, gets that vertical bar in between them. And I also need to put some parenthesis around this. So it's going to be Dark or Milk chocolate. That's our first code. Let's go and try it out. Find that and it finds Milk chocolate, Find again and it says it's completed.
Well I know there are some other instances of Dark chocolate and Milk chocolate in this document; it's a whole magazine about chocolate after all. So I need to find out why it's not working. So here is the key. GREP is case-sensitive. You always have to pay attention to the case of every character. So in this case it's looking for D, M and c, but if change that to a C and hit Find, now it will find it. It will find Milk Chocolate, click Find again, there is Milk Chocolate and there is Dark Chocolate. So it will find either Dark or Milk Chocolate.
Now if I want to tell that GREP code to search for lowercase or uppercase, I need to add a few more codes in here. So I'm going to put in some more characters. This is going to be either lowercase or uppercase d. Did you see that? I put in an open parenthesis and then a lowercase and then a vertical pipe and an uppercase d and then closed parenthesis, and I'll do the same thing here for the m, lowercase or uppercase m. And I'll do the same thing for the c. This is little bit more verbose than you need to be. There is more codes in here than you actually need, but it's a very easy way to see what's going on.
Dark chocolate, lowercase or uppercase, lowercase m or uppercase and lowercase or uppercase c. So now we have a lot of flexibility in here. It's going to be looking for Dark or Milk chocolate in uppercase or lowercase characters. Let's try it out. Find it, find it, find it. There we go. It's going to keep looking around and there we go. There is the Dark chocolate in lowercase. Both d and c are in lowercase here as well. I want to point out one little trick here that I need to warn you about. If I use the Option+Spacebar or Alt+Spacebar trick to use my grabber hand to try and scroll this over, I'm going to have a problem and that problem is that my GREP code will disappear. This always catches me. It's very, very frustrating.
For example I'll do Option+Spacebar and I'll start to be able to scroll around, but look what happened to my GREP code. It completely disappeared. That is very frustrating. Maybe it's a bug in InDesign, I don't know. But it actually typed a bunch of spaces here because the GREP code was selected. Fortunately there is a way to get those codes back. So if I click on this little pop-up menu with the double arrows, I can see all of the previous Find/Change routines that I've done. In this case the Dark chocolate and Milk chocolate, GREP that I was choosing. So I'll just load it back in there. Very, very easy to get that back into the Find/Change dialog box.
Just something to watch out for. Okay. Now it's time to do a little bit more complex GREP expression. I'm going to find all the words in my document that start with a letter c and end with e. So I'll type c in here and I'll type e here. But what's going to go in between the two? I'll click my cursor right between the c and the e. What's going to go in between there? Well, I need to type some codes that mean 'Find me some more characters.' I don't know what those codes are necessarily but the good news is that InDesign will type them for me.
I'll go over to this @ sign flyout menu and I'm going to choose a wild card or a meta character. The wild card that I'm going to choose is Any Letter. So I'll choose that and you can see that it types some codes for me. It did a square bracket, \l\u, and then closed square bracket. Well what does this mean? I'll tell you. It means a lowercase character or an uppercase character and because it's in square brackets it means one or the other of these. This is another way to do a this or that. In GREP there are multiple ways of doing the same thing.
But in this case it's only going to find three letter words that's start with c and end with e. We want it to be a four-letter or five-letter or eight-letter word or whatever. So we need to type one more code in here, a repeat code. So I'll go back to the flyout menu here and go to the Repeat submenu and I'm going to be going into much more detail about the Repeat submenu later on. But for right now I'm just going to tell you I'm going to choose One or More Times and that types in a little + code here. So now we have a code that says start with a c, then I want to have a string of one or more lower or upper case characters and then end with an e.
Now I'm going to tweak this code one more way. I want that to be lowercase or uppercase c. So I'll go ahead and type that square bracket, cC, closed square bracket, and again, this is just a different way of doing the same kind of this or that that we saw earlier. c or C followed by lower or uppercase characters, one or more of them and then ending with an e. All right let's try it out and see if it works. Find. There it goes. It found a word that starts with a c and ends with e. Find the next and this is great because it was also lowercase or uppercase. Remember? So it found that chocolate as well. Find Next, Find Next, Find Next. And I can just keep finding them until it finds this word, look at that. It even found coffee because coffee starts with c and ends with e.
Now there is one more thing I need to tell you about GREP codes and that is you are going to make mistakes. Everybody makes mistakes when doing GREP. I'm making mistakes while working on this title right now. In fact, if you're not seeing my mistakes, it's because the editors cut them out, but everyone makes mistakes, and that's okay. That's what undo is for. Just undo, Command+Z or Ctrl+Z on Windows and then look at your codes again carefully and then try it again.
- Using pre-built find/change expressions
- Reordering names in an exported list from a database
- Working with GREP and fonts
- Establishing character formats with GREP style
- Using escape characters to customize code
- Accessing the fly-out menus to specify search commands
- Applying global conditions using GREP