Using GREP styles in InDesign
GREP is a command line text search utility originally written for Unix, and the acronym means globally search a regular expression and print. Essentially, GREP in InDesign allows you to use code to find and edit or style text through the Find/Change dialog box or Paragraph Styles. GREP is complex and can be intimidating, but it’s not terribly difficult to learn the basics, and InDesign offers tools to make using GREP easy. Because of the depth of the subject, I’m only going to address GREP styles in this article.
GREP in Paragraph Styles
Let’s say you’re working on promotional materials for a business and you want to put the company’s name in bold throughout the text. You could create a character style for that, but then you have to find the company name everywhere it appears and assign the style manually. Instead, you can create a GREP style within the paragraph style which will automatically find and style the text for you. I already have a body text style created, so I’ll go to the Paragraph Styles palette and edit the Style Options for the body text style (either double-click on the style name, or select the body text style and use the flyout menu at the top of the palette to select Style Options). Click on GREP Style in the side menu, then click on the New GREP Style button.
It appears as if there’s no way to select from here, you have to actually click on [None] to get the dropdown menu to show up. So click there and you can either select an existing character style or create a new one. I’ll go to New Character Style, then Basic Character Formats and select Bold in the Font Style menu. Since this is a nested style, it’ll retain all the other formatting of the body text style. I’ll also click the No Break check box so that the company name never splits on to two lines, and just to make it really easy to see I’ll go to Character Color and change the color to magenta. I’ll call name this character style company name and then click OK.
I need to specify what text to assign this style to, so I’ll click on the \d+ so I can enter Smith Associates in that field. I have the Preview box selected, so as soon as I click off of that text field, I can see that Smith Associates is now bold and magenta throughout the body text.
GREP Expressions
Now say you want to assign a style to all the email addresses within the text. This is where GREP expressions come into play. I’ll go back into the body text Style Options and choose GREP Style and create another character style just like before, only I’ll call this one email addresses and make it italic/magenta/No Break.
Since the email addresses won’t all have the same text, you need to enter a GREP expression, so you need to think out how to build that. One thing that all email addresses will have is the @ sign, so you can start by entering that in the text field. Then you’ll need to set up code to tell it to find something with text on either side of that @ sign, and here’s where InDesign helps you. That @ symbol with the arrow to the right of the text field is a dropdown menu that’ll automatically enter the codes for you. Go to Wildcards and select Any Letter, and the code [\l\u] will appear in the text field – that means any lowercase or uppercase letter. That literally means only one letter, though, so I need to go to Repeat and select One or More Times. There might also be numbers, so I’ll click inside the brackets and go to Wildcards – Any Digit, and I’ll enter a period in there, too (all this has to be inside the brackets).
Now I can copy and paste the bracketed part and plus sign and paste that in front of the @ sign, so the entire code is: [\l\u\d.]+@[\l\u\d.]+Â There might also be underscores and other characters that you’d need to enter, you might need to do a little trial and error to make sure you’re getting it right.
Escape Characters
GREP uses a number of common symbols for coding expressions. For instance, if you wanted to write a code to find something at the end of a paragraph, you’d use the dollar sign – $. So if you wanted to style prices, you couldn’t just type in a dollar sign. To get around this, you can “escape” the coded meaning for the character by placing a backslash in front of it, so your code to represent a dollar sign in an expression would look like this: \$
Resources
This just scratches the surface, there’s much more that GREP can do for you. The website InDesign Secrets has a lot of tips and information about GREP in InDesign. The Online Regular Expression Testing Tool is a great way to test out expressions, and the right sidebar has a glossary of expressions. Now you know enough to get started, so don’t be afraid to try it!