GREP searches in InDesign CC 2018 and earlier is a strange new world for many of us. The ability to search for text patterns immediately suggests new and powerful possibilities. Here are some things I have learned about GREP.

I frequently do battle with client-supplied Word docs, textfiles, web pages texts, and even email texts. No matter what the source, the keystroking is almost always in a bad state, and needs either a lot of cleanup, or else a whole lot of cleanup!

You already know that we generally go over text to remove double spacebar spaces, extra returns, and the like. InDesign CS2 and newer already have a cleanup JavaScript that makes quick work out of that. If you haven't tried it yet, look for FindChangeByList already installed by default for CS3/CS4/CS5/CS6/CC/CC 2014/CC 2015/CC 2017/CC 2018. If you are still using CS2, you have to go digging in the Resource Disk to find, install, and use the pre-made JavaScripts.

Some will be familiar with going to the Find/Change dialog box and doing a whole series of finds and changes to cure common typing ills. Already, you will like the fact that in CS3 and newer, you can save and name your Find/Change queries. That becomes a time-saver.

Now in CS3/CS4/CS5/CS6/CC/CC 2014/CC 2015/CC 2017/CC 2018 comes GREP, which stands for General Regular Expression Parser. This is an advanced, pattern-based search technique to search for and replace text and formatting. Now you can do more ambitious text cleanup and tagging operations than ever before.

The world of programming is foreign to me, the graphic artist. So getting my head around this hast been challenging. But after reading Peter Kahrel's PDF book "GREP in InDesign CS3/CS4" from O'Reilly (with updates covering CS5), I think I'm beginning to see the light. What I wonder is: Why aren't there already a bunch of pre-installed saved GREP queries for the average designer to use? Most of the time, we all want to do the same common text cleanup operations.

Instead, there are pre-installed just a few saved GREP queries to get you started, and to whet your appetite for going deeper. Here are a few things I have tried and saved as reusable queries. (If you have made something clever, send it to me at This email address is being protected from spambots. You need JavaScript enabled to view it. Also, if you want a handy look-up sheet of GREP symbols, click here.

General Regular Expression Parser

Useful GREP search examples

1. Find ALL CAPS paragraphs in order to change to TitleCase or SentenceCase

Find: \u\u\.\r or possibly \u\u\.$

Replace with formatting: a paragraph style of your choosing

Note: When someone types with the caps lock key on! This looks for two capital letters, followed by a period, followed by a hard return. It doesn't replace it; just applies a paragraph style. This was useful on a textfile I worked on where every untagged subheading was typed in all capital letters.

The follow-up step is to run Dave Saunders script called ChangeCaseofSelectedStyle.jsx. You can find this posted on the InDesignSecrets.com site, under the Plug-Ins and Scripts section of the site. Double-clicking this installed JavaScript brings up a dialog box. You choose the Paragraph Style and the case to change it to.

 

2. Find ALL CAPS words

Find: \<[A-Z]{2,}\> or better, \<[\u]{2,}\>

Replace: with formatting: OpenType All Caps

Note: The minimal length {2,} is to prevent it picking up all single capitals (remove if you want those as well). Ideally, you search full caps with this regular expression and replace them with OpenType All Caps. Any other way will need handwork or a script. For international text, you should search for \<\u{2,}\> (the \u uppercase wildcard also finds capital "Ü")

 

3. Find capital letters like acronyms and smallcap them

Find: \u\u+ or Find: >\<\u\u+\>

Replace: with formatting; such as SmallCaps

Note: This finds 2 or more uppercase letters. The \< and \> are word delimiters, so it won't alter anything else than caps words. If the Replace field is empty and it has formatting, it applies the formatting to the found text. Only if there is also no formatting, it replaces with nothing.

 

4. Swap First Names and Last Names

Find: (.+)\t(.+)

Replace: $2~h $1

Change Format Settings: Your paragraph style that nests bold until the End Nested Style Here metacharacter

Search: Selection

Note: This GREP pattern looks for two subexpressions where any character appears 1 or more times. They are separated, perhaps, by a Tab character. The Replace positions the second found subexpression first, followed by an End Nested Style Here metacharacter, a spacebar space, and the first found subexpression last. Along with the applied paragraph style that calls for a nested character style of bold, you have the reversed names where the Last name appears in bold.

 

5. Find and delete soft return line breaks

Find: \s?\n

Replace: with single spacebar space

Note: Soft returns replaced by spacebar space

 

6. Find quoted words and change to italic

Find: (~{)(\u*\l*\s*.*\w*\d*)(~})

Replace: $2 and italic character style formatting

Note: This finds the quotes but leaves them out since it only changes to found group 2; not 1 and 3.

 

7. Find all email addresses

Find: [\l\u\d_%-]+@[\l\u\d_%-]+

Replace: with formatting

Note: The _, %, and hyphen were added because those characters are not included in the wildcard codes. The square brackets group the codes together. The + indicates that these characters can appear any number of times, or not at all. The @ sign is outside the group to appear only once. The group is then repeated for the rest of the address.

 

8. Capitalize first letter of word after bullet

Find: (.)(\t)(\l)

Replace: $1$2$3

Note: In the Replace formatting set text to uppercase

 

9. Search for words with brackets

Find: \[\w{4,20}\]

Replace: with formatting

Note: This finds with brackets

 

10. Search for words within brackets

Find: (?<=\[)\w{4,20}(?=\])

Replace: with formatting

Note: This finds within but not including brackets

 

11. Search for phone numbers to regularize

Find: \d?[-. ]?
\(?(\d{3})\)?[-. ]?(\d{3})[-. ]?(\d{4})

Replace: $1.$2.$3

Or Replace: $1-$2-$3

Or Replace: ($1) $2-$3

Note: This finds 3 digits, then three digits, then four digits after the hyphen. The ? symbol indicates that the element can exist zero or one time in the string, so the code [-. ]? means that a hyphen, dot, or space may or may not exist between the digits. Replace can use periods, parentheses, or hyphens, as you wish.

 

12. Search for dates to format

Find: \d\d?-\d\d?-(\d\d)?\d\d

Replace: with formatting

Notes: This sequence finds dates formatted like 09-04-2008 as well as 9-4-08

 

13. Replace multiple terms with one term

Find: \b(illustration|graph|map|chart)

Replace: figure

Note: The list of alternative words must be separated by the vertical bar, and each alternative is replaced with the replace term. GREP is case sensitive by default; so to replace case-insensitively, add (?i) before the expression.

 

14. Undocumented negation in Wildcards

Not documented in the Wildcards flyout is that the uppercase version of the wildcards negates them.

Note: Thus, \D finds everything that is not a digit; \U finds everything that is not an uppercase letter, and \L finds everything that is not a lowercase letter, etc.

 

15. Find underlining and replace with italics

This uses Text; not GREP, and is good because character styles control better than direct formatting.

Note: Have pre-built character styles for bold, italic, and bold italic. Also, have superscript, smallcaps, and other character styles defining swatch colors pre-built for use in Find/Change as well as nested styles.

 

16. Interpret GREP into English

RegexBuddy (http://www.regexbuddy.com/)

Notes: To see the translation, click the Create tab after entering a regular expression. You will see that the program has listed your expression as a sequence of icons/explanations. Windows-only.

 

If you have other immediately useful GREP searches, let me know. Don't forget to click the little diskette symbol in order to save any GREP queries. These become small .xml files located in specific folders on your computer. Their locations are listed below:

Mac OS: Users\[username]\Library\Preferences\Adobe InDesign\[Version]\Find-Change Queries\[query type]

Windows XP: Documents and Settings\[username]\Application Data\Adobe\InDesign\[Version]\Find-Change Queries\[query type]

Windows Vista: Users\[username]\AppData\Roaming\Adobe\InDesign\[Version]\Find-Change Queries\[query type]

Bear in mind that since they are saved .xml files, you can copy them and email them and install them on anyone else's computer, regardless of platform.

 

Mike Witherell

Adobe Certified Expert in InDesign, InCopy, Photoshop, Acrobat, and Illustrator

This email address is being protected from spambots. You need JavaScript enabled to view it.

200 OK

OK

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, [no address given] and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.