
A friend called me with what I thought was a simple request: find a way to search an InDesign document for spelled numbers, like one thousand forty-seven and twelve hundred six. “No problem,” I said naively, and concocted a simple GREP search expression that he could enter into InDesign’s GREP Find/Change dialog, thinking that would be the end of it. Instead, I soon discovered that this was a much harder problem to solve, and began a long journey to try and find a complete answer to the problem.
First, a bit of contextualization. In most technical, scientific, and other long and complex documents, the common editorial convention is to spell numbers less than 10, except for fractions and decimals. There are slight variations in this practice, depending on audience, country, and other variables. For more details, see This article on Wikipedia and this one on Grammarly.com.
The problem my friend had was several hundred pages of text where the authors had spelled out numbers greater than ten, and he took on the daunting challenge of reading the text to locate them and replacing them with numbers (change two thousand four hundred twenty-two to 2422, for example).
This turns out to be a much more complex problem than it first appears, and for some reason, like a dog with a bone, I couldn’t pass it up. The problem is that the English language is very irregular when it comes to how we spell numbers, with many exceptions. Here are some examples :
- 21 can be written twenty one Where twenty one
- 1202 can be written one thousand and two, one thousand and two, twelve hundred twoWhere twelve hundred two.
- Years are expressed differently than numbers, as seventeen seventy-six Where twenty twenty one.
Then, to make this even more difficult, I thought it would be useful if we could also search for the numbers 0-9 when they aren’t spelled out, because they generally should be, except when…
- In fractions, like ¾, ½etc
- In decimal fractions, such as .seven Where 2
- Before a comma followed by another digit, like 1,234
And for good measure, I decided that we should also search 1st, 2nd, 3rd, etc as these should be replaced by First second third, etc
Each case I describe above poses a particular problem in GREP. But if you’ve ever written a GREP expression, you know that the little Find field in InDesign’s Find/Replace GREP dialog box is ridiculously small (Figure 1). And the GREP expression I was creating was quickly getting huge.

Figure 1. The small Find What field in the GREP Find/Change dialog box makes entering and editing complex GREP expressions very difficult. There must be a better way!
No problem, I thought. The brilliant Peter Kahrel has created a cool script called the GREP Editor that makes typing long GREP strings a lot easier (Figure 2).

Figure 2. Peter Kahrel’s GREP editor script makes typing long GREP strings much easier. You can even include spaces, returns, and comments to make your GREP string clearer. This usually works fine, but the length of the GREP expression I was writing pushed it to the limit.
Unfortunately, as my GREP string got longer and longer, it started crashing InDesign 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/Replace dialog (Figure 3).

Figure 3. Screenshot of part of the script that splits the very long GREP string into eight smaller parts.
This approach allowed me to break the extremely long GREP string into eight logical parts for clarity and testing purposes, then join them into a 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 some useful features like highlighting parenthesis pairs that make working with complex strings much easier.
This ability to break the GREP chain into smaller pieces also allowed me to be more verbose. In other words, there is arguably a shorter and “cleverer” way to write the GREP string than the one I finally came up with (Figure 4). But for complex tasks like this, I believe a simple, verbose result trumps a smart, everyday result. It’s much easier to review and debug or modify later.

Figure 4. The final GREP expression. Much too long to modify in the Find/Replace dialog box!
You can download and use the finished script here. Read the included instructions on how to install and use the script. After all the effort I put into it, I hope someone finds it useful!