• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

GREP search: change style within parentheses

Contributor ,
Jul 23, 2022 Jul 23, 2022

Copy link to clipboard

Copied

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

TOPICS
How to , Scripting , Type

Views

2.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Jul 23, 2022 Jul 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

Votes

Translate

Translate
Community Expert , Jul 24, 2022 Jul 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.

 

 

Votes

Translate

Translate
Community Expert ,
Jul 23, 2022 Jul 23, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 24, 2022 Jul 24, 2022

Copy link to clipboard

Copied

Thanks! Works as expected.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 24, 2022 Jul 24, 2022

Copy link to clipboard

Copied

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.

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 24, 2022 Jul 24, 2022

Copy link to clipboard

Copied

Thanks! Works as expected as well. A big relief. Hundreds of faculty names with (department name) that has to be italicized. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 31, 2024 Jul 31, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 31, 2024 Jul 31, 2024

Copy link to clipboard

Copied

Try this: 

 

//\K.+?(?=//)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 01, 2024 Aug 01, 2024

Copy link to clipboard

Copied

Thanks so much! It worked perfectly - really appreciate you taking the time to reply

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 31, 2024 Jul 31, 2024

Copy link to clipboard

Copied

This should work for a find. Change to whatever you want to change the text to. 

(?<=//).+?(?=//)

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 31, 2024 Jul 31, 2024

Copy link to clipboard

Copied

Great minds think alike!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 01, 2024 Aug 01, 2024

Copy link to clipboard

Copied

I'm old school and never picked up on the use of \K

 

What would be the time when \K is better or different than positive lookbehind, out of curiosity. It's a gap in my GREP knowledge.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 01, 2024 Aug 01, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 01, 2024 Aug 01, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 01, 2024 Aug 01, 2024

Copy link to clipboard

Copied

Yes. Do this:

 

Find what: //(.+?)//

Change to: $1

Change format: Italics

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 01, 2024 Aug 01, 2024

Copy link to clipboard

Copied

Thank you! This is brilliant - much appreciated

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 16, 2024 Sep 16, 2024

Copy link to clipboard

Copied

Great, these examples! Does anyone know how to include the parentheses in the GREP style?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 16, 2024 Sep 16, 2024

Copy link to clipboard

Copied

LATEST

Found it:

 

(?=\().+?(?<=\))

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines