Join Michael Murphy for an in-depth discussion in this video Specifying exact matches and ranges, part of Learning GREP with InDesign.
The one or more times metacharacter matches the most text, and possibly too much, and the Shortest Match option will limit that to the least amount of matching text. But what if you need something in between those two extremes? Suppose you need to match something exactly two times, or two or more times, or at least two times, but no more than four times. You won't find an option for that in the Repeat Metacharacters menu or anywhere in InDesign's help documentation. But it's very simple to do so. I am going to open Find/Change using Command+F, switch to the GREP tab and suppose I wanted to find any two or more numbers.
I will go to the Special characters menu and under Wildcards, I will choose Any Digit and from the Repeat submenu, I can do One or More Times, I can do Zero or One Time, I can do One or More Times (Shortest Match), but there's nothing here that lets me find something exactly two times. I could go in and just type another \D metacharacter, choose Find and I will find two numbers. There, there and there! But that can get kind of tedious.
That can be a lot of metacharacters in a very small field. If I want to describe something an exact number of times, I just need to put that number in braces or what's called curly brackets. I will type in an opening brace, the number 2 and a closing brace and I have now described any two digits. If I click Find, that's what I get. If I wanted to find any two or more digits, I just type a comma after that number 2 and when I click Find, it's now found three digits and here it finds two and here it finds three again.
So that's changing my starting point. It's not one or more. This is a way of saying two or more. If I change that to three and clicked Find, I would find three or more digits. If I wanted to get very specific and define an exact range, for instance, at least two, but no more than four digits, I could change the 3, here, to a 2. That's my first value, and then after the comma, I'll type a 4. So now I'm describing any digit at least two times but no more than four times and if I click Find I will find the 499 here, I will find two digits here, three digits here, two here, two here and so on, throughout the document.
Specifying exact ranges with this method lets you build more accurate expressions than you can with the more broad options available from the Repeat Metacharacters menu.
- 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