beautypg.com

Tips for constructing grep searches, Grep search examples – Adobe InDesign CS5 User Manual

Page 172

background image

166

USING INDESIGN

Text

Last updated 11/16/2011

Tips for constructing GREP searches

Here are some tips for constructing GREP expressions.

Many searches under the GREP tab are similar to those under the Text tab, but be aware that you need to insert
different codes depending on which tab you’re using. In general, the Text tab metacharacters begin with a ^ (such
as ^t for a tab) and GREP tab metacharacters begin with a \ (such as \t for a tab). However, not all metacharacters
follow this rule. For example, a paragraph return is ^p in the Text tab and \r in the GREP tab. For a list of the
metacharacters used for the Text and GREP tabs, see

Metacharacters for searching

” on page 169.

To search for a character that has symbolic meaning in GREP, enter a backslash (\) before the character to indicate
that the character that follows is literal. For example, a period ( . ) searches for any character in a GREP search; to
search for an actual period, enter “\.”

Save the GREP search as a query if you intend to run it often or share it with someone else. (See

Find/change items

using queries

” on page 175.)

Use parentheses to divide your search into subexpressions. For example, if you want to search for “cat” or “cot,”
you can use the c(a|o)t string. Parentheses are especially useful to identify groupings. For example, searching for
“the (cat) and the (dog)” identifies “cat” as Found Text 1 and “dog” as Found Text 2. You can use the Found Text
expressions (such as $1 for Found Text 1) to change only part of the found text.

GREP search examples

Follow these examples to learn how to take advantage of GREP expressions.

Example 1: Finding text within quotation marks
Suppose you want to search for any word enclosed in quotation marks (such as “Spain”), and you want to remove the
quotation marks and apply a style to the word (so that it becomes Spain instead of “Spain”). The expression

(")(\w+)(")

includes three groupings, as indicated by parentheses ( ). The first and third groupings search for any

quotation mark, and the second grouping searches for one or more word characters.

You can use the Found Text expressions to refer to these groupings. For example, $0 refers to all found text, and $2
refers to only the second grouping. By inserting $2 in the Change To field and specifying a character style in the Change
Format field, you can search for a word within quotation marks, and then replace the word with a character style.
Because only $2 is specified, the $1 and $3 groupings are removed. (Specifying $0 or $1$2$3 in the Change To field
would apply the character style to the quotation marks as well.)