Join Michael Murphy for an in-depth discussion in this video Text searching vs. GREP searching, part of Learning GREP with InDesign.
One way to begin understanding what GREP is capable of is to compare it to the text-based search and replace functions most of us are used to. A standard text only Find/Change query is literal. It looks for exact text and replaces it with other exact text. For example, you might search for every instance of dog and replace it with cat. But one GREP search can look for every instance of dog, or cat, or hamster, or parakeet, and replace all of them with pet in one step.
That's an example of a condition, or is a condition this or that or another thing or something else. Text searching is also limited. While you may be able to search for any digit using a wildcard in InDesign's text-based Find/Change, it only finds one digit at a time. With GREP, however, searches can be flexible, so you could describe any digit, one or more times, and find single, double, triple, quadruple numbers, and so on.
A text search looks only for characters, letters, numbers, spaces, punctuation and so on, but GREP can also look for locations, allowing you to search based on where text is, not just on what it is. GREP can also remember the text that it finds and reuse it later on, no matter how unique that specific text is, allowing you to rearrange that text, eliminate parts you don't need and add parts that you do. Primarily, GREP parses text for patterns and most text can be described as a pattern.
Take, for example, a U.S. phone number, which is made up of ten digits. That number is usually broken up into smaller units of a three-digit area code, a three number exchange, and the last four numbers. So while you and I may think of a phone number as something like this, GREP thinks of that number this way, any three digits, a dash, any three digits, another dash, and four digits. That's a pattern. The numbers themselves don't matter and that's the real power behind GREP because this pattern will describe and find any U.S. phone number.
And if some of them use dashes, but others use periods or someone close the area code in parentheses but others don't, GREP can handle that too. After you've used GREP a few times, you'll find you start to see patterns where you never noticed them before and as you get more confident with some of GREP's more advanced expressions, you'll be able to anticipate and account for inconsistencies in those patterns and describe any kind of text accordingly.
- Using metacharacters, the building blocks of GREP
- Describing text that may not exist with zero operators
- Applying multiple character styles to the same text with GREP styles
- Eliminating orphaned words at the ends of paragraphs
- Preserving and recalling subexpressions
- Customizing a GREP-based text cleanup script for long documents