Sie sind auf Seite 1von 5

InStep: GREP

How to grok GREP By Sandee Cohen

The new GREP tab in InDesign You know you’re a geek when you get excited about most common problems in imported text. We’ve
CS3’s Find/Change opens a the Find/Change dialog box. But this dialog box has
long been a powerful tool for efficiently handling large
also included a file with the raw text. This file,
named “GREP Example,” is attached to this PDF
powerful set of commands. amounts of text. Now, in CS3, the Find/Change dialog document. Open it and you can follow along.

1
box has gotten a complete overhaul, and it’s not just
a little better… it’s total geek heaven. Not only can Examine the text for searchable patterns
you now search for object attributes (such as color, The text in each of the text frames is
strokes, and so on) and special glyphs, InDesign CS3 typical of the kind of text that might
lets you use GREP expressions for use in search strings. come in from email documents. We need to
GREP (which
���������������������������������������������
stands for General Regular Expression apply the Signature paragraph style to the text
Parser) wasn’t
����������������������������������������������
invented for InDesign CS3. Programmers that is preceded by an em dash (�Figure 2)�.
have used GREP for years to wrangle complicated
search-and-replace operations within files.
With GREP, you can build searches that contain
specialized substitutions and wild cards. For instance,
you can look for all the text between parentheses
and apply a character style. This seemingly simple
task is basically impossible without GREP.
Chances are, you’re not a programmer. So
why should you care? Because once you get past
the mindset that you don’t need GREP, you’ll be
amazed by how much time you’ll save. And since
the Adobe engineers have included premade
search strings for some of the most common search
requests, you don’t even have to know the codes. Figure 2: The text contains many strings of characters and non-
printing characters that need to be cleaned up. Our first task is to
This tutorial, which documents the creation of restyle the text that comes after the em dashes..
a brochure for a fictitious Hawaiian visitor center
Figure 1:The imported text in this three-fold brochure spread is full
of weird characters typical of text from email messages. Removing (Figure 1), will help you understand how to use
the characters and reformatting by hand would be tedious. GREP expressions to quickly clean up some of the

Subscribe Today! next page » full screen 


CS3 Preview Article
MAGAZINE
InStep: GREP

Tip: Save your queries


Once you have designed your own searches, you can
easily save them for future use. (This isn’t just GREP
What makes this a GREP challenge is that we searches, it also includes, Text, Glyph, and Object a tiny trashcan). Then click Change All to delete all
don’t want to style any text that simply contains an searches.) the angle brackets that are followed by one or more
em dash within the regular body text paragraphs. Just click the Save Query icon next to the Query list. spaces. Your text should now reflect the changes
This opens the Save Query dialog box where you can

2
shown in the portion of text shown in Figure 4.
name your query.
Use the new Find/Change dialog box
The query then appears inside the preset list. This
As soon as you choose Edit > Find/Change, list is divided into four sections. The first section is for
you’ll notice the tabs that let you select each Text searches, the second for Grep searches, the third
of the four types of searches (Figure 3). The Text for Glyph searches, and the fourth for Object attribute
tab gives you controls similar to previous versions searches.
What’s really cool about this list is when you choose
of InDesign. The GREP tab gives you the controls
one of the presets, the tab automatically changes to the
for regular expression searches. The Glyph tab lets correct type of search.
you search and replace specific glyph characters.

3
The Object tab lets you search and replace object
attributes. To do GREP searches, click the GREP tab. Delete multiple characters and spaces
Our next cleanup task is to delete the
angle brackets. It’s simple to find those
brackets; type > in the Find What field.
However, there may be more than one bracket on Figure 4: The text as it appears after the quoted angle brackets and
each line. With a simple Text search, we’d have to run spaces are deleted.

4
the search several times. With GREP we can set the
search to look for multiple instances of the brackets. Find characters in position
Choose Repeat > One or More times from the We can find the code for the em dash in
Special Characters for Search menu to insert a + GREP searches by choosing Symbols >
after the bracket. (Of course, once you know that Hyphens and Dashes > Em dash from the Special
code, you could just type the + character yourself.) Characters for Search menu (Figure 5). This
This find several angle brackets in a row. inserts the code ~_ into the Find what field.
Figure 3: The four category tabs, Text, GREP, Glyph, and Object, in However, we also need to delete the spaces after However, there are other em dashes within the
the Find/Change dialog box let you switch between each of the the angle brackets. So type a space and then another text that we don’t want to convert. So we need
types of searches..
+ sign (indicating one or more space characters). to specify only those em dashes that begin
Leave the Change What field blank and click the paragraphs. Put your insertion point at the front
Clear Specified Attributes icon to the right of the of the Find what field (before the ~_ code) and
Change Format section (that’s the icon that looks like choose Locations > Beginning of Paragraph.

Subscribe Today! « previous page next page » full screen 


CS3 Preview Article
MAGAZINE
InStep: GREP

Tip: Wrangling Telephone Numbers


A slick way to learn how to work with GREP is to use one
of the presets under the Query list and then dissect the
code. My favorite is the Phone Number Conversions (dot
Format field. (If you don’t see the Change Format format).
field, click the More Options button in the Find/ The Find code is \(?(\d\d\d)\)?[-. ]?(\d\
Change dialog box.) Then choose the paragraph d\d)[-. ]?(\d\d\d\d). This finds the three digits
style you want to apply; in this case, Signatures. for a US area code, then the next three digits before the
The Find/Change box should appear as hyphen and then the next four digits after the hyphen.
What makes this code particularly clever is the use of
shown in Figure 6. Click the Change All dialog the ? symbol which indicates that the .elements can ex-
box to apply the Signature paragraph style. Even ist “zero or one”time in the string. So the code [-. ]?
though the Change to field is blank, it does not means that a hyphen, dot, or space may or may not exist
Figure 5: The Special Characters for Search menu makes it easy to delete the text in the Find what field because between the digits.
insert the GREP codes into the Find what or Change to fields. The Change code uses the codes $1.$2.$3. The
we’ve told InDesign to apply formatting.
code $1 inserts the items indicated by ( )’s sets in the

5
Find field. The code $2 inserts the second items. And the
This inserts a ^ in front of the em dash code: Delete spaces that may not be there code $3 inserts the third items.
^~_, which means “look for all em dashes at the It’s rather simple to delete the forced line Using these found codes allows you to add a a period
beginning of a paragraph.” We can then click the breaks (sometimes called “soft returns”): Choose in between the digits.
Specify Attributes to change icon for the Change Forced Line Break to insert the GREP code \n. The
challenge is that some of those line breaks have If we replace all line breaks with spaces, we could
a space in front of them while others don’t. If we get two spaces in a row, requiring another cleanup.
simply delete the breaks that aren’t followed by a GREP gives us the flexibility to search for
space, we could have two words jammed together. something that may or not exist. The ? code says that
something can exist zero or one time. We can also use
Tip: Watch the codes
the Wildcards > Any White Space to insert the code
Many searches under the Text tab are similar to those \s. This ensures that we capture all types of spaces.
under the GREP one. But you need to insert different With the expression \s?\n we look for any
codes depending on which section you’re in. white space that may or may not be there, followed
For instance, under the Text section a tab character is ^t, by a line break that will definitely be there.
while the GREP code is \t. But if you think that the only
difference is the caret or the slash, look at a paragraph re- We then enter a single space in the Change field.
turn. In the Text searches, the code is ^p, but it’s \r under This means that only one space will replace the
the GREP section. See the table attached to this PDF for a line breaks. For those line breaks that don’t have
complete list of all the Text and GREP search codes. a space before them, it will avoid word collisions.
If you’re like me and you enter your codes manually, in- For those line breaks that do have a space before
stead of using the search menus, you need to remember
Figure 6: By leaving the Change to field blank, you can assign a under which section you’re working. them, it avoids creating multiple spaces.
paragraph style to all the text in the paragraph.

Subscribe Today! « previous page next page » full screen 


CS3 Preview Article
MAGAZINE
InStep: GREP

6
lowercase letters (\l), any digits (\d), any spaces (\s),
Delete multiple returns any character (.), or any word characters (\w). If we
Here’s another task that could be handled in an add the * after each character code, it will find any of
ordinary Text search: Look for two paragraph those characters that may appear zero or more times.
returns and replace them with a single return. However, this code isn’t quite ready to do what we
But, as anyone who has ever done such a search want, as you’ll see in the next step.

8
knows, you have to rerun the search several times
when there are three or more returns in a row. Change just the text inside the quotes
Happily, Adobe included a preset query to With the code as it stands in the previous step,
handle this situation in a single search. You can any changes we apply in the Change Format
choose the preset by using the Query menu Figure 8: Now we’ve taken care of those pesky forced line breaks.
field will be applied to all the found text. But
�������
we
at the top of the dialog box (Figure 7). need to style just the text within the quotes.
When you choose Multiple Return to Single The + sign indicates that the second break Fortunately, there’s a way to specify just one
Return, InDesign inserts the strings for both character appears one or more times. So if there is part of the found text that should be changed.
the find and the change fields. ~b finds a another break character after the second one, that We need to separate the code into three groups
standard carriage return; \r looks for that and character will also be included in the found text. using parenthesis. So we now write the code
column breaks, frame breaks, and so on. Now, your text should look like Figure 8. as (~{)(\u*\l*\s*.*\w*\d*)(~}).

7 Find text with quotes


Our last cleanup task is the hardest: We need
to find a certain text string, delete part of
the found text, and style the remaining part. Look
Tip: Searching for an email address
Suppose you want to find all email addresses in text and
apply a special character style. [\l\u\d_%-]+@[\l\
u\d_%-.]+ is the code that will find any valid email
address. The \l wildcard finds any lowercase letters.
at the words “Waikiki Park.” We want to apply the \u finds any uppercase letters. \d finds any digits. The
Italic character style to the text within the quote. period finds characters such as periods. The underscore,
But we also want to delete the quotation marks at percentage sign, and hyphen were added as those
the same time. The trick is to use a wildcard, which characters are not included in the wildcard codes.
remembers a portion of the found text so that The square brackets tell InDesign to search for any one
of these characters. The + indicates that these charac-
we can “insert” it again in the Change to field. ters can appear one or more times. The @ sign is outside
We can get the code for quotation marks under the the group, as it can appear only once. The group is then
submenu for Quotation Marks. The code for a double repeated for the rest of the address.
Figure 7: The Query menu comes with presets for different Find/
left quote mark is ~{. The code for double right quote Once the email text has been found, it’s simple to use
Change routines. You can also save your own queries. mark is ~}. We can use the submenu for Wildcards to the Change To formatting to apply a character style.
insert the codes for any uppercase letters (\u), any

Subscribe Today! « previous page next page » full screen 


CS3 Preview Article
MAGAZINE
InStep: GREP
Figure 10: InDesign has replicated the text and formatting changes
you see here on every page of the brochure .

In that code string, the first parenthetical group is


for the open quote. The second group is for any found
text within. The third group is for the close quote.
Once we’ve divided the code into these groups,
we can insert the code $2 (or choose Found 2 under
the Found Text menu) into the Change to field to style
only the second group. This not only lets us style
just the inside text, it also automatically deletes the
text that was in groups 1 and 3 (Figure 9). One page
of the finished result of our efforts is in Figure 10.

Figure 9: As if by magic, the quotation marks disappear, and


InDesign italicizes the text that was in-between them .

Going Further with GREP


While GREP may seem daunting, if you start with basic Sandee Cohen is the author of InDesign CS3 Visual Quickstart
exercises such as these, you can quickly work up to Guide and a columnist for InDesign Magazine. She is also a frequent
speaker for the InDesign and Creative Suite conferences.
very sophisticated searches. I did it, and I’m hardly a
code geek. In fact, if I can grok GREP, anyone can!

Subscribe Today! « previous page next page » full screen 


CS3 Preview Article
MAGAZINE

Das könnte Ihnen auch gefallen