Showing posts with label GREP. Show all posts
Showing posts with label GREP. Show all posts

Monday, September 13, 2010

Two essential books for advanced InDesign users

If you do any InDesign scripting (or want to learn), or create any InDesign GREP searches (or want to learn), you need these eBooks by Peter Kahrel. If it were possible for eBooks to become "dog-eared", mine would be. I refer to one or both of these books almost every week.

Scripting InDesign CS3/4 with JavaScript is an 80-page concise tutorial on how to automate InDesign with JavaScript. Through clear explanation and lots of examples, Peter explains how to write and edit JavaScript for use in InDesign, and provides a good overview of the InDesign Document Object Model.

GREP in InDesign CS3/CS4 is a 65-page tutorial and reference guide for how to do complex, powerful searches in InDesign using GREP.

Despite the titles of these guides, Peter has recently updated both of them to cover InDesign CS5, as well as earlier versions. These are the only two published guides that I know of that cover these two subjects...I'm glad that Peter has done such a great job.

Wednesday, December 02, 2009

An InDesign GREP tutorial

Steve (who attended my "Power through your work with InDesign Styles" seminar in September) wrote:

"I am trying to find an InDesign GREP style that would find select every character (paragraphs included) between any amount of tabs and the next instance of a bullet. Any Ideas?"

The GREP string that I came up with to do what Steve asks is this:

(?<=\t)[^~8\t]+(?=~8)

I'll take this apart and try to help you understand it piece by piece.

(?<=\t)

The \t means to look for a tab character. The (?<=) is called a "positive lookbehind," which tells InDesign to not include the thing it finds in the search result. So (?<=\t) means to "look back" for a tab character, but don't include the tab character in the search result.

[^~8\t]+

The square brackets create what is called a "character class," which means to "match any of the things in these brackets". In this case, in the brackets we have a ~8 which is a bullet, and a \t which is a tab character. So [~8\t] would mean to match any character that is a bullet or tab. But if you look closely, we have a ^ before the bullet in the character class. This means "not". So [^~8\t] means to match any character that is NOT a bullet or tab.

Finally, you'll notice a + after the character class. This means to match the character class "one or more times". So [^~8\t]+ means to match "one or more characters that are not bullet or tab characters".

(?=~8)

As we saw above, the ~8 is a bullet character. The (?=) is a "positive lookahead," which, like a "positive lookbehind" tells InDesign to not include the thing it finds in the search result. So (?=~8) means to "look ahead" for a bullet character, but don't include the bullet character in the search results.

Putting it all together, we have (?<=\t)[^~8\t]+(?=~8) which means to:

Look for one or more characters that are not bullets or tabs, that are preceded by a tab character and followed by a bullet, (but don't include the tab or bullet in the search results).

The (?<=\t)[^~8\t]+(?=~8) string can be used in InDesign in two different places:

1. The GREP tab of the Find/Change dialog box, to do a search based on the GREP expression, or

2. The GREP Style tab of the Paragraph Style Options dialog box, which allows you to have InDesign assign a character style automatically any time that the GREP expression is matched.

Monday, October 12, 2009

Use GREP find to truncate lines of text

A student from one of my on-site InDesign training classes asked how to remove the end of each line of text in an InDesign-created directory listing, as shown in the example below. Each line of the directory consists of some text, a tab character, more text, and then a paragraph return.

In her email, she said "there are thousands, nay, millions of these pesky things...can Indesign find and replace wildcard strings?"

InDesign CS3/CS4's GREP Find/Change feature is made for just this sort of thing. Choose Edit > Find/Change, and fill in the dialog box as shown below.

In this case, \t represents a tab character, and the plus sign after it means to look for "one or more" tab characters. The period stands for "any character", and the asterisk after the period means to look for "zero or more" of "any character". So the entire Find phrase means "look for one or more tab characters that may or may not be followed by additional characters." Since the "Change to" field is empty, the found text will be replaced by nothing.

Tuesday, October 02, 2007

Use GREP to find URL's

In a previous Blog entry I wrote about the GREP search capability in InDesign CS3. GREP searches are great for making a computer recognize complex patterns. I recently developed the following GREP string for a client, and thought I'd share it here, hoping you might find it useful or educational.

The procedure below will allow you to quickly search for all URL's (Web addresses) in your text, and format them as non-breaking, or blue, or whatever formatting you wish.

1. Start InDesign, and choose Edit > Find/Change

2. Type the string below into the Find what: field (all on one line)

(?i)(http|ftp|www)(\S+)|(\S+) (\.gov|\.us|\.net|\.com|\.edu|\.org|\.biz)

3. Copy the string below into the Change to: field

$0

4. Click on the More Options button, and then click on the magnifying glass icon next to the Change Format area. Enter whatever formatting you would like here, then click the OK button.

5. Click on the small disc icon at the top of the Find/Change dialog box, and give the Query a name. Now, any time that you want to find and change URL's, you can just choose this name from the Query drop-down list, indicate the scope of the search, and click on the Find button. Another GREP example is here.

Wednesday, August 15, 2007

Formatting phone numbers in InDesign

In a previous Blog entry I wrote about the new GREP search capability in InDesign CS3. Today I hammered out the following GREP string for a client, and thought I'd share it here, hoping you might find it useful.

The procedure below allows you to search for U.S. phone numbers in almost any format, and replace them with numbers in the format (800) 555-1212 with a non-breaking space after the area code and a non-breaking hyphen before the last four digits. This is a variation on the GREP string included with InDesign that formats phone numbers in "dot" format.

1. Start InDesign, and choose Edit > Find/Change

2. Copy the string below into the Find what: field.

\(?(\d\d\d)\)?[-.\s~~]?(\d\d\d)[-.\s~~]?(\d\d\d\d)

3. Copy the string below into the Change to: field

($1)~S$2~~$3

4. Click on the small disc icon at the top of the Find/Change dialog box, and give the Query a name. Now, any time that you want to find and change phone numbers you can just choose this name from the Query drop-down list, indicate the scope of the search, and click on the Find button.

Friday, June 08, 2007

GREP searches in InDesign CS3

The Find/Change dialog box in InDesign CS3 has several new features, including the ability to do "GREP" searches. GREP searches use “regular expressions” to allow you to search for patterns in text. For example, you could use a GREP search to find all the email addresses in a document and make them blue, or find all telephone numbers, regardless of format, and reformat them with periods like 888.555.1212.

Today, while preparing a quick reference guide for one of my classes, I needed to search through a file that contained about 1200 paragraphs, and delete the 900 or so paragraphs that contained the phrase "none defined". Entering ^.*(none defined).*$ into the Find what field of the GREP section of the Find/Change dialog tells InDesign to find each occurrence of the phrase "none defined" and then select the entire paragraph containing the phrase. In the Change to field I entered nothing, clicked on the Change All button, and was done. A huge time saver.

The GREP syntax is pretty twisted. If you like programming, scripting or Sudoku you will be able to learn GREP as needed. Here are a few resources to get you started:

First, visit the GREP section of the Adobe help resource center. Then download a handy GREP quick reference guide in PDF format. Issue #17 of InDesign Magazine has a great non-technical article by Sandee Cohen on how to begin learning GREP. Here is good GREP reference site.

If you don't care to tackle learning GREP , at the very least, keep a text file on your hard drive that contains examples, like the one above, that you can copy and paste as you need them. You don't have to understand GREP to use examples that closely match what you need.