Skip to main content
Inspiring
June 3, 2025
Answered

GREP expression help

  • June 3, 2025
  • 2 replies
  • 409 views

Hi. I need help resolving a GREP issue. I need to apply superscript to reference numbers and the commas or endashes within, but NOT the commas separating the names. For example:

Karen Smith4, Frank Jones2–5, Johnboy Walton12, Harry Bottom10,11.

I have this

\d[–, \d]*?

And it recognises the name separating commas, but not the ones needed between the reference numbers (2–5 or 10,11).

This is for a scientific application, and unfortunately, I must keep the style (commas and endashes), not switch them out for hyphens or semicolons. 

Correct answer Eugene Tyson

@Barb Binder did a great job - but if you're finding it's not catching all then here's a slightly more robust version

 

(?<=\w)\d+(?:[–,]\d+)*\b

 

(?<=\w): makes sure it only matches numbers after a letter/word (like a surname)

\d+: the starting reference number

(?:[–,]\d+)*: allows any number of ,number or –number combos

\b: ends at a word boundary (avoids accidental overshoot)

2 replies

Barb Binder
Community Expert
Community Expert
June 3, 2025

Hi @Catharine26941291c64a:

 

We are always happy to help.

 

I will get my head around these things someday

If you don't yet own a copy of Peter Kahrel's book GREP in InDesign, Third Edition, I highly recommend obtaining one. It's a great learning tool and a great reference. 

 

~Barb

~Barb at Rocky Mountain Training
Barb Binder
Community Expert
Community Expert
June 3, 2025

Hi @Catharine26941291c64a:

 

How about

\d+(,\d+)?(~=\d+)?

 

~Barb

~Barb at Rocky Mountain Training
Inspiring
June 3, 2025

Perfect!! Thank you. I will get my head around these things someday 🙂

Eugene TysonCommunity ExpertCorrect answer
Community Expert
June 3, 2025

@Barb Binder did a great job - but if you're finding it's not catching all then here's a slightly more robust version

 

(?<=\w)\d+(?:[–,]\d+)*\b

 

(?<=\w): makes sure it only matches numbers after a letter/word (like a surname)

\d+: the starting reference number

(?:[–,]\d+)*: allows any number of ,number or –number combos

\b: ends at a word boundary (avoids accidental overshoot)