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: June 2015. Author Peter Kahrel updated this Short Cut to cover InDesign CC. Several examples have been added, and most examples are now analysed in more detail.

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. Cover Page
  2. Title Page
  3. Copyright Page
  4. Detailed Contents
  5. Power Searching with GREP
  6. GREP by Example
  7. The Basics: The Find/Change Dialog
    1. Adding Formatting and Styling
    2. Undocumented Features
  8. 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
  9. Locations
  10. 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?
  11. Repeating a Pattern a Specific Number of Times
  12. Referring to Wildcards: Back-Referencing
  13. Finding Formatted Text
  14. 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
  15. 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
  16. Chaining GREP Queries
  17. Applying Styles with GREP Find/Change
  18. Look Ahead: Match Selectively
    1. Adjust Space Between Certain Characters
    2. Small Caps Revisited
    3. Negative Lookahead
  19. Look Behind
    1. Italicize Prefixed Words
    2. Italicize Letters Following a Year
    3. Use Table Figures After Capitals
    4. A Limitation of the Classic Lookbehind
    5. Negative Lookbehind
  20. Lookaround
    1. Match Words Wrapped in Certain Characters
    2. Keep Certain Words Together
    3. Replace hyphens in page ranges—revisited
  21. Lookaround and Formatting
    1. Changing Formatting to Text Tags
    2. Changing Text Tags to Formatting
  22. Replacing Using Location Markers
  23. Single-line and Multiline
  24. Conditional GREP Expressions
  25. 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. Temporarily Disabling GREP Styles
    7. GREP Styles, Nested Styles, and Nested Line Styles
    8. Copying GREP Expressions from Queries to GREP Styles
  26. Troubleshooting
    1. Patch to InDesign’s Latest Version
    2. Escape Characters
    3. GREP Is Case-Sensitive
    4. Hyphens in Character Classes
    5. The Dot Does Not Match Footnote Markers
    6. Parentheses
    7. Split Complex Expressions
    8. Spaces
    9. You’re Trying a GREP Search Using the Text Search Tab
    10. Try GREP Expressions on a Small Sample
    11. Character-Class Mismatches
  27. Resources
  28. 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