O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

GREP in InDesign

Book Description

Updated: August 2010. Author Peter Kahrel updated this Short Cut to cover InDesign CS5.

Updated: November 2009. Author Peter Kahrel updated this Short Cut to address typos and reader comments.

GREP (short for "General Regular-Expression Print") is a powerful tool that lets you use wildcards ("jokers") to search and replace text. InDesign's GREP implementation can be used for text and also for formatting codes, finding patterns in text as well as literal text.

GREP moves beyond the restrictions that hampered earlier InDesign search features, but unfortunately it does have the reputation of being difficult to master. As with many things, it can be challenging to learn, but, fortunately, a lot can be done with surprisingly simple expressions. The aim of this Short Cut is to show how to create simple but powerful regular expressions.

Table of Contents

  1. GREP in InDesign
  2. Power Searching with GREP
  3. GREP by Example
  4. The Basics: The Find/Change Dialog
    1. Adding Formatting and Styling
    2. Undocumented Features
  5. Wildcards (or Character Classes)
    1. Any Character
    2. Any Uppercase Letter
    3. Any Lowercase Letter
    4. Any Letter
    5. Any Digit
    6. Any Word Character
    7. Accented Letters
    8. Accented Letters: Character Equivalents
    9. Digraphs
    10. Any Whitespace
    11. Any Dash
    12. Quotation Marks
    13. Any Punctuation
    14. Opening and Closing Punctuation
    15. Homemade Wildcards: Character Classes
    16. Negative Character Classes
    17. Alternatives: Sort of a Wildcard
  6. Locations
  7. Repeat: Sequences of Characters
    1. ? Zero or One Time
    2. * Zero or More Times
    3. + One or More Times
    4. Shortest Match: Lazy Operators
    5. Alternative: Negative Character Class
    6. Shortest Match Or Negative Character Class?
  8. Repeating a Pattern a Specific Number of Times
  9. Referring to Wildcards: Back-Referencing
  10. Finding Formatted Text
  11. Replacing with Wildcards
    1. Swap First and Last Name
    2. Swap Last Name and Initials in a Selection
    3. Change First Names to Initials
    4. Change Date Format
    5. Remove Dots at the End of Section Headings
    6. Remove Duplicate Entries in a List
    7. Replace Certain Hyphens with an En Dash
  12. Splitting Up Complex GREP Expressions
    1. Replace Hyphens in Page Ranges with En Dashes
    2. Drop Digits
    3. Inserting Thousand Separators
    4. Keeping Initials Together
  13. Chaining GREP Queries
  14. Applying Styles with GREP Find/Change
  15. Look Ahead: Match Selectively
    1. Adjust Space Between Certain Characters
    2. Small Caps Revisited
    3. Negative Lookahead
  16. Look Behind
    1. Italicize Prefixed Words
    2. Italicize Letters Following a Year
    3. Use Table Figures After Capitals
    4. Lookbehind Limitation
    5. Negative Lookbehind
  17. Lookaround
    1. Match Words Wrapped in Certain Characters
    2. Keep Certain Words Together
    3. Replace hyphens in page ranges—revisited
  18. Lookaround and Formatting
    1. Changing Formatting to Text Tags
    2. Changing Text Tags to Formatting
  19. Replacing Using Location Markers
  20. Single-line and Multiline
  21. Conditional GREP Expressions
  22. GREP Styles
    1. Ordering GREP Styles
    2. Apply Fonts to Unicode Ranges
    3. Applying Styles in a Table of Contents
    4. GREP Styles: Efficiency
    5. How To Optimize GREP Expressions
    6. Avoid * Whenever You Can: Use + Instead
    7. Use [^ ]+ Instead Of .+? Wherever Possible
    8. Use Location Whenever Possible
    9. Don’t use single-character alternatives: use a character class
    10. Don’t Use ( ) If You Don’t Have To
    11. Use Non-Capturing Groups
    12. Temporarily Disabling GREP Styles
    13. GREP Styles, Nested Styles, and Nested Line Styles
    14. Copying GREP Expressions from Queries to GREP Styles
  23. Troubleshooting
    1. Patch to InDesign’s Latest Version
    2. Escape Characters
    3. GREP Is Case-Sensitive
    4. Hyphens in Character Classes
    5. Parentheses
    6. Split Complex Expressions
    7. Spaces
    8. You’re Trying a GREP Search Using the Text Search Tab
    9. Try GREP Expressions on a Small Sample
    10. Character-Class Mismatches
  24. Resources
  25. Quick Reference
    1. Character Representations
    2. Character Classes 1: Standard Classes
    3. Character Classes 2: Posix Expressions
    4. Character Classes 3: Unicode Properties
    5. Location Markers
    6. Modifiers
    7. GREP Code Dictionary
  26. Copyright