September 18 2009 • 8:30 AM

Add a Digit with GREP Groups

Jen wrote,

I am working on a catalogue where every 5 digit item number needs to be changed to reflect a zero in the third spot of the existing number, to make it a 6 digit number. Is there any automated way I can do this?

Sure! As long as you’re using InDesign CS3 or CS4, and the numbers are all editable live text (that is, they’re not part of placed images or anything). You can use the GREP panel in Edit > Find/Change to add the zero.

Let’s say you want to add a zero in the third spot of these SKUs:

Open Find/Change and click the GREP tab (again, only available in CS3 or CS4). In Find What, we want to find five digits in a row, but since we’re going to want to put something in the middle of the number, we need to split the Find What into two groups of digits. That way, when we do the Change to, we can say “put the first group back, then add a zero, then put the second group back.”

To find “any digit” via GREP we use the simple code \d (backslash d). To group what GREP finds, we wrap each group in parentheses. So to find any five digits in a row, defined as one group of 2 digits immediately followed by a group of three digits, we enter this:

Then in the Change To field, we can refer to each parenthetical group by the shorthand “$1″ (no quotes) for the first group, “$2″  for the second group, and so on. To add characters in between the two groups, we just enter them literally, or if they’re special characters we escape them with the backslash. So to tell InDesign to add a zero in between the two groups of digits, we enter the code for “first group, zero, second group:”

Running the Find/Change on the example results in this:

… which solved Jen’s problem. She wrote back, “Thank you so much!!! That worked beautifully!”

2 Responses discussing this post. Add yours below.

  1. September 19th, 2009 • 12:30 pm • Link

    GREP is absolutely marvelous! Is there anything it can’t do?

    Know anyone in OS X development at Apple? Snow Leopard deliberately introduced only a few new features, which suggests that they’ll be open to introducing many new features in 10.7.

    It’d be great if they radically improved the long-neglected text handling features that Cocoa offers developers. And one of the best new features would allow them to build GREP search and replace into virtually every text application. Done right, GREP S&R could useful features such as:

    1. A prepackage set of useful S&R routines like those that come with ID.

    2. The ability to share S&R routines between applications and between users online.

    3. A built-in ‘input this, output that’ display as part of the S&R screen. Rather than mess with actual text and maybe get something wrong, a user setting up a GREP S&R could type text into one field and see the result instantaneously in another field. Change the GREP and the ‘result’ output updates automatically.

  2. Jongware
    September 21st, 2009 • 8:41 am • Link

    I’ve complained at times about Adobe’s implementation of GREP (they missed out on a few things I could use…) but here’s a real funny one that works (note the dollar-less lookup!):

    (\w)(\w)(\w)\1\2\3

    will find words of type “barbarian”, “ringing“;

    (\w)(\w)(\w)\3\2\1

    will find “grammar“, “bruttura”. You can make them as long as you want :-) and even scan for syllables:

    (\w\w)\1 finds “training”, “superstitious”.

Subscribe to the Discussion

Get the ongoing discussion surrounding "Add a Digit with GREP Groups" delivered to you. Click here to subscribe via RSS.

Leave a Reply

You can use limited HTML tags, such as <em></em> for emphasis/italics and <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .

InDesignSecrets reserves the right to edit and/or remove posts and comments.