is now part of CreativePro.com!

InDesign GREP Essentials: Quantity

7

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.

Erica Gamet has been involved in the graphics industry for over 35 years. She is a speaker, writer, trainer, and content creator focusing on Adobe InDesign, Apple Keynote, and varied production topics. She is a regular presenter at CreativePro Week, regular contributor to CreativePro Magazine, and has spoken at Canada’s ebookcraft, Adobe MAX, and Making Design in Oslo, Norway. Find Erica online at the CreativePro YouTube channel, CreativeLive.com and through her own YouTube channel. When she isn’t at her computer she’s probably daydreaming about travel or living in a Nordic noir landscape.

Website
LinkedIn
Instagram
  • Many thanks. I loved the IDS podcast you were in. GREP is so great, I keep nagging Apple to bring it into the text services of macOS, so every text app can have it. Apple could make it system-wide, so the same search and replace works in all text apps. And it could create a good way to share them among users. And yeah, that’d mean Apple would have to pay attention to people who work for a living not just texting twits.

    Ah, but there is a text-matching issue that I’ve not been able to accomplish or find anyone who has. That’s to find the entirity of HTML links in every case. A few years back, I tried to find one and drew a blank. Finding the start is easy. Just look for “html:”. But no GREP search I found could reliably locate the end of the link. They would fall short, done in by some character in that HTML link. I coped by extending the selection manually, but that defeats much of the purpose of using GREP.

    Any ideas?

    At times I wonder if GREP should be revised and have specific commands to locate various linking codes, including mailto and html, taking into account their quirks. Much of GREP was created before the modern Internet and all the demands it places on users seeking to edit texts.

  • Erica Gamet says:

    If I’m reading that correctly, you want to find the actual URL in an InDesign document? One that starts with http or https? This is what I’ve used in the past:

    https*://\H+

    I know it even works on those super long Amazon links. Let us know what you’re specifically looking for, though.

    Glad you enjoyed the podcast!

  • Peter Laely says:

    Thanks Erica. nice explanation.
    what stands the \H for?

  • >
    Notice: We use cookies on our websites to give you a great online experience. If you keep browsing, we'll assume you're ok with this. For more information, see our privacy policy. By closing this banner, you agree to the use of cookies.I AGREENo