Skip to main content
Inspiring
June 13, 2024
Answered

GREP style to add a colon

  • June 13, 2024
  • 3 replies
  • 984 views

I have a document with Chapter headings (eg 'Chapter One') and what I want is to add to the Chapter Heading style a GREP expression that counts/misses the first two words and then adds a colon, ie making it 'Chapter One: '

Can that be done with GREP or am I asking the impossible?

This topic has been closed for replies.
Correct answer James Gifford—NitroPress

I can't guess why it would go backwards. 😛

 

There is probably a way to combine the two searches, but the simple solution to the hyphenated words would be to change the GREP string to (borrowing from MJ's model here)

Chapter\s\w+-\w+

 

ETA: And here's the combined string: Chapter [\w|-]+

 

I recommend Peter Kahrel's excellent book GREP in InDesign as a worthwhile reference to have at hand.

3 replies

Robert at ID-Tasker
Legend
June 13, 2024

@m5heath

 

Do you have a separate - dedicated - ParaStyle applied to those Chapter Headings?

 

m5heathAuthor
Inspiring
June 13, 2024

Yes I do - why do you ask?

Robert at ID-Tasker
Legend
June 13, 2024
quote

Yes I do - why do you ask?


By @m5heath

 

Sorry, I thought that you have only Chapter One, etc. and you want to add ":" at the end of the paragraph - then you could use "$".

 

But after re-reading your post - you have something else after that, right? 

 

So you want to insert ":" after "number" and before the rest of the title, right? 

 

But what others suggested - will only work up to "nineteen" and then whole tens... 

 

Community Expert
June 13, 2024

I don't think Grep Style or Nested Style would work for you, as they can only style the content and not add or remove content. You would have to use the regular find/change route in my opinion. But we would need to think of a way to restrict this only to the headings so search for the text as well the the style applied over the text. So the grep would be simple like

Find What :- (Chapter\s+\w+)

Replace With :- $1:

Find Format :- The name of the applied style

-Manan

-Manan
James Gifford—NitroPress
Legend
June 13, 2024

Fairly easily, although I have to say the subject line looks like something that belongs in a medical forum. 🙂

 

Would it be much easier, though, to just do a quick search for "Chapter" in the heading style, then type in the colon? That would probably take less time than copying a GREP style from here, even.

 

I don't think a GREP or Line style will do it; it would have to be a GREP search and replace. So... pretty much same either way.

 

But this should do it:

Note there's no space at the end of the GREP string.

m5heathAuthor
Inspiring
June 13, 2024

Many thanks for responding. Strangely, this GREP expression selects the next-to-last of 28 Chapter headings and then goes backwards through the headings despite my having 'Forward' checked - but it does then add the colon. That would be OK but there is another issue: from twenty onwards the number becomes 'Chapter Twenty-One' etc and the expression treats Twenty-One as two words. So the change becomes 'Chapter Twenty:-One'. Is there a way to treat a hyphenated word as one word? Sorry if I'm stretching this to breaking point!

James Gifford—NitroPress
Legend
June 13, 2024

I can't guess why it would go backwards. 😛

 

There is probably a way to combine the two searches, but the simple solution to the hyphenated words would be to change the GREP string to (borrowing from MJ's model here)

Chapter\s\w+-\w+

 

ETA: And here's the combined string: Chapter [\w|-]+

 

I recommend Peter Kahrel's excellent book GREP in InDesign as a worthwhile reference to have at hand.