is now part of CreativePro.com!

A Script to Find Spelled Out Numbers in InDesign

3

A friend called me with what I thought was a simple request: find a way to search in an InDesign document for numbers that are spelled out, like one thousand forty-seven and twelve hundred six. “No problem,” I said naively, and whipped up a simple GREP search expression that he could enter in InDesign’s GREP Find/Change dialog box, thinking that would be the end of it. Instead, I soon discovered that this was a much harder nut to crack, and I began a long journey to try to find a complete answer to the problem.

First, a bit of background. In most technical, scientific, and other long, complex documents, common editorial convention is to spell out numbers less than 10, except for fractions and decimals. There is some slight variation on this practice, depending on audience, country, and other variables. For more details, see this article at Wikipedia and this one at Grammarly.com.

The problem faced by my friend was several hundred pages of text where the authors had spelled out numbers greater than ten, and he faced the daunting challenge of reading through the text to locate these and replace them with numerals (changing two thousand four hundred twenty-two to 2422, for instance).

This turns out to be a much more complex issue than it first appears, and for some reason, like a dog with a bone, I couldn’t let it go. The problem is that the English language is highly irregular when it comes to how we spell out numbers, with lots of exceptions. Here are just a few examples:

  • 21 might be written as twenty one or twenty-one
  • 1202 might be written as one thousand two, one thousand and two, twelve hundred-two, or twelve-hundred and two.
  • Years are expressed differently than numerals, such as seventeen seventy-six or twenty twenty one.

Then, to make this even more challenging, I thought it would be helpful if we could also search for the numerals 0–9 when they aren’t spelled out, because they usually should be, except when…

  • In fractions, such as ¾, ½, etc.
  • In decimal fractions, such as .7 or 2
  • Before a comma followed by another numeral, like 1,234

And for good measure I decided we also need to search for 1st, 2nd, 3rd, etc. because those should be replaced with first, second, third, and so on.

Each case that I describe above poses a particular problem in GREP. But if you’ve ever written a GREP expression, you know that the tiny Find What field in InDesign’s GREP Find/Change dialog box is ridiculously small (Figure 1). And, the GREP expression I was creating was quickly becoming huge.

InDesign Find/Change dialog box showing a GREP expression for finding spelled out numbers

Figure 1. The tiny Find What field in the GREP Find/Change dialog makes entering and editing complex GREP expressions very difficult. There’s got to be a better way!

No problem, I thought. The brilliant Peter Kahrel has created a cool script called the GREP Editor that makes entering long GREP strings much easier (Figure 2).

Peter Kahrel's GREP Editor showing the full GREP expression for finding spelled out numbers written in English

Figure 2. Peter Kahrel’s GREP editor script makes typing long GREP strings much easier. You can even include space, returns, and comments to make your GREP string more clear. This usually works very well, but the length of the GREP expression I was writing pushed it over the edge.

Unfortunately, as my GREP string got longer and longer, it started to make InDesign crash if I tried to enter it using Peter’s script. After trying several different solutions, I ended up writing a script that loads the GREP string into the Find/Change dialog box (Figure 3).

A portion of the script code for breaking a very long GREP string up into eight smaller parts.

Figure 3. A screenshot of a portion of the script that breaks the very long GREP string up into eight smaller parts.

This approach allowed me to break the extremely long GREP string into eight logical parts for clarity and testing purposes and then join them into one final mega-string at the end of the script. I was also able to use my favorite code editor, Microsoft Visual Studio Code, to edit the GREP string. VSC has helpful features like highlighting pairs of parentheses that make working with complex strings much easier.

This ability to break the GREP string into smaller pieces also allowed me to be more verbose. In other words, there is doubtless a shorter, more “clever” way to write the GREP string than the one I finally arrived at (Figure 4). But for complex tasks like this, I believe that a result that is simple and verbose trumps a result that is clever any day. It makes it much easier to re-visit and debug or modify later.

The complete GREP expression for finding spelled out numbers in InDesign

Figure 4. The final GREP expression. Much too long to edit in the Find/Change dialog!

You can download and use the finished script here. Read the included instructions for how to install and use the script. After all the effort I put into it, I hope that someone finds it useful!

Keith Gilbert is a design consultant, developer, educator, speaker, and author. His work has taken him throughout North America, Africa, Europe, and Asia. During his 35+ year career his clients have included Adobe, Apple, Target, Oracle, and the United Nations. He is the author of several popular titles for LinkedIn Learning, Adobe Press, and CreativePro. Find him at gilbertconsulting.com and on Twitter @gilbertconsult
  • Awesome stuff, Keith! It’s weird how a friend asking for help can start you down an epic rabbit hole, isn’t it? There are those subtle moments along the way when curiosity turns into determination, determination into stubbornness, stubbornness into obsession (in my case, usually around 2 am). Like all good adventures, these micro-project-turned-saga things tend to be more enjoyable as stories than while they’re in progress!

  • Erica Gamet says:

    Like a shiny new Apple product, sometimes you don’t NEED a thing, but it’s so beautiful you just WANT the thing. I’m going to download this, and pet it, and love it, and maybe someday I’ll even USE it. Thanks, Keith… amazing work (as usual)!

  • Scott Kelty says:

    Ironically, I actually need the opposite – my employer has a policy that numbers 10 and under, when not in a table, mathematical expression, or address, need to be spelled out. That should be a bit simpler. :)

  • >