Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
One of my favorite usages for GREP is to apply Paragraph Styles and Character Styles to text in a long document. For example in this file I have some lists, and this list is just not consistent. I guess somebody actually typed 2 and then there is a period, and then a space, but up here there is a bunch of spaces and down here there is a number, a period and a tab. So in some cases there are tabs, in some cases there are spaces. I really wish I could get rid of all of that and just apply InDesign's automatic numbering. That way I know it will be consistent and look just right throughout the document.
So how can I do that? Well, I'll open up the Find/Change dialog box with the Command+F or Ctrl+F on Windows and I'm going to type in a GREP code that defines this pattern. This pattern is going to be a digit. So I know that's /d and I'm going to say -- actually there might be more than one digit because maybe it's one digit or maybe it's ten or eleven, maybe it's more than one digit, and I think that it has a little dot after it. Most of these seem to have a period, but I know that the period code means any character. So I need to escape it out.
I will press the left arrow key on my keyboard and put a \ before it. So this means a period itself, look for that actual character. Now it might be there or in some cases it might not be there. Maybe they forget to put in. So I'll put a question mark. So that says this period may or may not be there. Now their may or may not be a space there. So I'll do a \s, and there is probably one space or two spaces, or I don't know how many, let's go to the fly-out menu and choose Repeat Zero or More Times. So it might be there or it might be one, two, three... we don't know, and that's the asterisk code.
So that's looking pretty good. That seems to define the code we are looking for. Let's try it. I'll click Find and we can see it found that 5. That's good. Find Next and that seems to work there. Uh-oh! That's not working at all. What happened? It found a number right in the middle of a paragraph. Well, it was a number and it was followed by a space, but that's not something we want to find. We need to define the GREP code even further and specify exactly what we are looking for. In this case we need to tell the GREP code to only consider numbers at the beginning of a paragraph.
So I'll place the cursor before that initial backslash. I'm going to go out to this little @ sign fly-out menu and I'm going to give it a Location. The Location pop-out menu offers me five choices, Beginning of Word, or End of Word, or right out of Word Boundary. In this case we want to use Beginning of Paragraph and only Beginning of Paragraph, which is the caret symbol. So this pattern has to be at the beginning of a paragraph. Let's try it out. Click Find Next, and it finds it, click Find Next, and it finds it. So this seems to be working just right. Now what do we want to do to this GREP pattern.
Well, we want to apply a paragraph style to it. So I'm going to do click down here in the Change Format field, brings up the Change Format Settings dialog box and I'm going to say I want to apply my numbering list Paragraph Style to anything that matches this style. Let's go ahead and click Change All and see what happens. It went through my whole document and it seemed to work but I didn't quite work the way I wanted it to. It applied the style, that's great, but it did not get rid of the text that it found. I still have this 8 period space, space and so on.
So I wanted to get rid of it, and I left to Change To field blank so you would think that it would delete it, right? Well, true except that there is this little i here. That i means that this formatting applied to it and when you have Find What filled, Change To blank but the i here, some format applied to it, that means go ahead and leave the text there. So in order to make this work I need to do one more little thing. I need to search for one additional character. I'm getting ahead of myself here a little bit but I'm going to let you in on a secret and that is, I can take text from here and put it down here if you put it in parenthesis. And the text that I'm going to take is any single character. That period means any single character that it finds.
And because it's in parenthesis I can put that character down here with a special code and that code is $1. Now dollar sign up in the Find What field means it's a Location tag, it means only find it at the end of a paragraph. That's what dollar sign means. But dollar sign down here in the Change To field means something very different. It means take whatever was up here inside this parenthesis and put it down here in the Change To field automatically. All right, so I'm going to Undo this, Command+Z or Ctrl+Z to go back to where I was, and now we'll try it one more with this extra code. One more time it's going to search for at the beginning of a paragraph, look for one or more digits followed by maybe a period, maybe not, followed by maybe some space, maybe not, followed by anything. And then take that anything and put it down here and while you are at it format the whole paragraph. Will it work? With one click we can see that it formats the entire document quickly. We have deleted the text that we didn't want there and applied the paragraph formatting.
Now let's take our new found knowledge of setting location to do one more search here. I'm going to delete these and start from the beginning. I want to find only whole words that begin with c and end with e. So I'm going to say c and e, I know that's there. I know that there is any lower case character in the middle so that's going to be \l with a plus. So one or more other characters between the c and the e, but not just any c followed by some letters followed by e. Only ones where the c is at the beginning of the word and e is at the end.
So to do that I place my cursor before the c, I can say at the beginning. At the beginning is going to live in the Locations pop-up menu and we could use either Beginning of Word or Word Boundary, either one works. I'll choose Word Boundary here, and then I'll put another Word Boundary over here. So it says at the Word Boundary start with the c. At the other end of the Word Boundary end with an E. Let's try it. Click Find and it finds Chocolate, Find again, chocolate. Find again, and it keeps, oops. There is another one, starts with a c and ends with an e, but it's not chocolate. So I know that my pattern worked.
If you don't specify a Location code for your GREP code it will find it anywhere, in the middle, beginning, end of a paragraph, you don't know where. But when you put those codes in that's you specify exactly what you want to find and where you want to find it.
Get unlimited access to all courses for just $25/month.Become a member
119 Video lessons · 50754 Viewers
117 Video lessons · 38439 Viewers
113 Video lessons · 81487 Viewers
65 Video lessons · 11376 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.