InDesign GREP Essentials: Quantity

This article is part of a series of posts on using GREP in InDesign for beginners.

When writing GREP queries, you may need to indicate exactly how many times a certain character, punctuation, or operation occurs. This helps you tailor an expression to fit a specific pattern while excluding anything that doesn’t fit. When working with quantities in GREP, there are a few expressions worth learning. Some indicate a given value and others are a bit more fluid.

How to Match a Specific Number of Things with GREP

Using the Any Digit wildcard \d) we could search for a 10-digit number with \d\d\d\d\d\d\d\d\d\d. In fact, when I first started writing GREP expressions, that’s precisely what I did. It works, but there’s a better (and more efficient) way. To be more concise, express the item you’re looking for and follow it up immediately with an exact value or range. That’s done using the curly brackets (braces) { }.

So to find ten digits in a row, simply express it as \d{10}.

Or More

A value by itself within the brackets indicates that exact amount. Adding a comma after a value {10,} means “or more.”

Targeting a Range of Values

To indicate a range, list the two values in brackets with a comma separating them. So \d{2,5} means “from 2 to 5 digits” and would match numbers from 10 to 99999, as well as 01 through 09.

One or More

Occasionally, we have no clue how many times something might appear: maybe twice, maybe 200 times! To account for that, there are two other special characters to learn. If we know that the thing we are looking for appears at least once—and maybe many more times—we need to use the plus symbol +) to indicate “one or more times.”

A phrase that you will use often is .+, which means “any character one or more times.” For example, if you are looking for opening quotes, followed by text that can be comprised of any number of characters (what I refer to as “stuff”), followed by closing quotes, the “stuff” in-between the quotes is expressed as .+

Zero or More

The last expression to know for specifying quantity is the * (asterisk) character. Officially it means “zero or more times,” which sounds a little odd. This means that a character may be present, but it may not. So I call it the “or not” expression. If you were looking for strings of characters that composed of a 5-digit number sometimes preceded by a capital letter, you would use the expression \u*\d{5}. The asterisk after the \u indicates it may be there, but it may not. So this would match things like Z78901 and 12345.

Bonus tip: If you forget which expression does what, both + (one or more) and * (zero or more) live in the fly-out menu just to the right of the Find What and Change To fields in the Find/Change dialog box.

Bookmark
Please login to bookmark Close

This article was last modified on June 14, 2024

Comments (7)

Leave a Reply

Your email address will not be published. Required fields are marked *

Loading comments...