Fun with GREP #1

My new InDesign plaything is GREP. Don’t let the techno-acronym scare you…GREP is not only fun, but so useful that I wonder how we got along without it before. So, my next few tips will be GREP-related in some way…hopefully you’ll get a little taste of what GREP is and what you can do with it. GREP appears in two main places in InDesign CS4…and one in CS3. GREP first appeared in ID CS3 in the Find/Change dialog and with CS4, InDesign allows you to build GREP into styles!

GREP can be boiled down to strings of text that fit a particular pattern. Now, don’t hold me to a tech dictionary-worthy definition. What I mean is, use GREP to find patterns in text and either replace that text with something else or format/reformat it in a different way. If you use the Find/Change dialog, you’re making changes to existing type. This is great if your text is finished and you don’t expect any changes. If, however, you want to affect strings of text dynamically on-the-fly, you might want to build a GREP style into your paragraph styles. GREP is most useful when you have the need to change or style lots of text and there’s a distinguishable pattern to that text.

The first GREP tip I’m going to show you involves taking some text (maybe the info has been flowed in from an Excel spreadsheet or other database) where the info is there, but the style and layout is not. In this case, I have a list of phone numbers, but they are simply a string of 10 digits. They look nothing like phone numbers at all and I need to format them (I’ll be using a North American style for phone numbers, but this can be adapted to any layout).

phnumbersbefore

After importing the strings of phone numbers that look like this: 1234567890, I’ll want to do a find/change to make them look like this: (123) 456-7890. Go to the Edit menu and choose Find/Change. In the dialog that opens, choose the GREP tab near the top. The first thing we need to define is the pattern of text InDesign needs to look for. You’ll need to know some of the GREP codes for items you’re looking for (which you can find online), or you can choose from the pull-down menu (with the @ symbol). In this case, we’re looking for digits and we know there are ten of them. But we also know we need to group them in a specific way to look like phone numbers. We know there will be three digits inside parenthesis followed by a space, then three more digits, a dash, and the last four digits. We’ll set these groups apart in our search by using parenthesis. We can use the pull-down menu to tell ID to look for “any digit” by choosing it (Wildcards>Any Digit) or we can type the GREP code “d”. So the code we will type in the “Find What” field is:

(ddd)(ddd)(dddd)

Now we need to tell it what to change the text to (our formatted numbers). To indicate that you want to find a grouped set of text and format it as a group, use the $ symbol. The groups are numbered in order of appearance. So, our first group is $1, the second is $2 and the last is $3. Still with me? Now let’s format how we want the final text to look…we type:

($1) $2-$3

grepfindchange

So, the first group (of three digits) will appear in parenthesis, the second group will follow a space and have a dash after it, and the third group (of four digits) will directly follow the dash. Choose “Change All” in the Find/Change dialog and the phone numbers will be formatted.

phnumbersafter

Click on the little disk icon to the right of the Query pull-down menu, and save this search for future use! You’ll also find a list of common Find/Change needs, like “Multiple Space to Single Space” and “Dash Dash to Em Dash.”

The next tip will look at incorporating GREP into paragraph styles for on-the-fly formatting. Now go have some fun with GREP!
Return to Tips and Tricks