Skip to main content
Fritz Hansen
Inspiring
July 23, 2022
Answered

GREP search: change style within parentheses

  • July 23, 2022
  • 2 replies
  • 4251 views

I have a document that includes long lists of names, many of which are followed by words in parentheses that need to have a certain character style applied (but the parentheses remain unstyled). Several years ago I asked if there is a GREP search that can accomplish this, and I received an answer from Nigel Chapman that worked:

 

Because you want to exclude the brackets from the matched string, you need to use look-behind and look-ahead. It isn't pretty. Search for

(?<=\()[^)]+(?=\))

Set the replacement to

$0

and set the Change Format to your character style.

 

It doesn't seem to be working now. Should this still accomplish what I need, or is there a change to the Find or Change To strings I need to make?

 

Mac OS 12.4

InDesign 17.3

 

Thanks

This topic has been closed for replies.
Correct answer Peter Kahrel

As Brian mentioned the $) isn't necessary. But why your expression doesn't work isn't clear, it works fine over here (with or without the escape that Brian mentioned).

But the expression uses two ways to stop at the first closing parenthesis, one is good enough. You can use either of these two:

 

(?<=\()[^)]+
(?<=\().+?(?=\))

 

In the first one,  [^)]+ stands for 'while not )', and in the second one,  .+?(?=\)) stands for 'repeat until the first next )'.

P.

 

 

2 replies

Peter KahrelCorrect answer
Community Expert
July 24, 2022

As Brian mentioned the $) isn't necessary. But why your expression doesn't work isn't clear, it works fine over here (with or without the escape that Brian mentioned).

But the expression uses two ways to stop at the first closing parenthesis, one is good enough. You can use either of these two:

 

(?<=\()[^)]+
(?<=\().+?(?=\))

 

In the first one,  [^)]+ stands for 'while not )', and in the second one,  .+?(?=\)) stands for 'repeat until the first next )'.

P.

 

 

edwardc52369671
Participating Frequently
July 31, 2024

Hello, I've found this thread as I'm trying to write a Grep which changes the text between two sets of double slashes.

i.e. it be word 'text' in this case

 

//TEXT//

 

I've been struggling to adapt the grep from this thread. It seems more complicated due to the double charcater, and the fact the opening and closing are the same characters.

 

Any help very appreciated

 

Many Thanks

edwardc52369671
Participating Frequently
August 1, 2024

(?<=. . .) doesn't allow variable-length matches, \K does. So ^\d+\K works, but (?<=^\d+) does not.


Out of interest, is there a variation which can also remove the two sets of // // so just the word in between them remains?

 

I'm still trying to get my head around Grep

 

Again - Many Thanks

brian_p_dts
Community Expert
July 23, 2022

You don't need the $0. You can leave the ChangeTo field blank. But this code with an extra escape in the negative expression line works [^\)]. Can you provide a sample line where that doesn't work? 

Find:

 

(?<=\()[^\)]+(?=\))

 

 Change Format: Your character style

Fritz Hansen
Inspiring
July 24, 2022

Thanks! Works as expected.