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

[GREP] Help find punctuations between brackets

Contributor ,
Jan 27, 2022 Jan 27, 2022

Copy link to clipboard

Copied

Hi guys,
I'm having difficulties understanding my problem here. If anyone can help, I'll be very thankfull 😉

I would like to find any punctuation between brackets in order to apply a condition on them (via a script). But my grep doesn't work 😕

I thought I could modify another grep expression, the following one, that is looking for everything between brackets. This one works fine and I'm using it a lot.

(?<=\().*?(?=\))



If I consider using the posix [[:punct:]], why the following expression doesn't work, what should be done in order to fix it ?

(?<=\()[[:punct:]]*?(?=\))

 

Thanks for your insight


Example: (need to get the 2 commas between the brackets, but not the one outside)

Hello Indesign folks (men, women, possibly lost aliens), I need your help.

TOPICS
Scripting

Views

1.8K

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 , Jan 27, 2022 Jan 27, 2022

Oh I see,  you have more than one group of parantheses... In this case use this:

(\(.+?\K|(?R))*?,(?=[^()]+\))

 

Votes

Translate

Translate
Community Expert , Dec 01, 2022 Dec 01, 2022

Do you mean somthing like that?

\[.+|\G.+?\K,(?=[^\[\]]*\])

Votes

Translate

Translate
Community Expert ,
Jan 27, 2022 Jan 27, 2022

Copy link to clipboard

Copied

That is only going to work if there is nothing but punctuation between the parentheses. Can you show us some examples of the actual text you will be searching?

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 ,
Jan 27, 2022 Jan 27, 2022

Copy link to clipboard

Copied

Thanks for you reply.

The overall goal is to use a script that sorts alpha words separated with commas. The script makes it done as long as there aren't any unwilling commas in the sentence, like the one that appear between parentheses.

I'd like write a script that can "neutralise" every commas thar are between the parentheses. The main frame of the script is done, except the Grep expression for finding the commas.

 

Ex 1: I'd like to be able to 

I can easily sort the words :

- Punching, Licking, Throwing

because they are "simply" separated by commas.

 

Ex2: I cannot use my script yet with the folowwing sentence

- Punching (Boxing, Wing Chun, Karate), Kicking (Kickboxing, Taekwondo, Capoeira, Savate), Throwing (Hapkido, Judo, Sumo, Wrestling, Aikido)

because of the commas appearing between the parentheses. 

I'd like to write a grep expression that allows me to select the commas between:

Boxing, Wing / Chun, Karate / Kickboxing, Taekwondo / Taekwondo, Capoeira / Capoeira, Savate / Hapkido, Judo / Judo, Sumo etc.

This way, I could apply a conditional text that will "hide" the commas. I'll then apply my sort script and delete the contion for the whole text to reappear.

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
Participant ,
Jan 27, 2022 Jan 27, 2022

Copy link to clipboard

Copied

try this

(?<=\\().*?(?=\\))

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 ,
Jan 27, 2022 Jan 27, 2022

Copy link to clipboard

Copied

That's the one I'm using (see my first post) but selects the whole text between parentheses.

For some reasons (linked to the behavior of the sort script) I need to focus on only looking for the commas.

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 ,
Jan 27, 2022 Jan 27, 2022

Copy link to clipboard

Copied

Try this one...

 

(\(.+?\K|(?R))*,(?=.+\))

 

 
If you need  to select more than just the , use this one and add your punctuations inside the [ ] like so.

 

(\(.+?\K|(?R))*[,.;:](?=.+\))

 

  

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 ,
Jan 27, 2022 Jan 27, 2022

Copy link to clipboard

Copied

Merci Jean-Claude pour ta réponse,

 

A hell of an grep !

Very close to what I'd like to achieve, but…

The expression reaches every single punctuation (which is already good, something I didn't succeed to) but the thing is, I'd like to reach only the ones between parentheses. 

I'm still not much familiar with the \K and \R and wonder what to modifiy to narrow the search between the parentheses

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 ,
Jan 27, 2022 Jan 27, 2022

Copy link to clipboard

Copied

This GREP only select the , when between parentheses. 

Have you try it in Find/Change or GREP Style?
\K or (?R) might not be supported in javascript

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 ,
Jan 27, 2022 Jan 27, 2022

Copy link to clipboard

Copied

I tried with Find/change. 

weird…

 

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 ,
Jan 27, 2022 Jan 27, 2022

Copy link to clipboard

Copied

here's a video that shows 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
Community Expert ,
Jan 27, 2022 Jan 27, 2022

Copy link to clipboard

Copied

Oh I see,  you have more than one group of parantheses... In this case use this:

(\(.+?\K|(?R))*?,(?=[^()]+\))

 

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 ,
Jan 27, 2022 Jan 27, 2022

Copy link to clipboard

Copied

I knew you were a genius, now, I experience it ^^

Works as a charm. Thank you very much !

Will see soon enough if this still works with javascript ^^

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 ,
Jan 27, 2022 Jan 27, 2022

Copy link to clipboard

Copied

… and I have to do my homework in order to understand this quite complex grep expression. There is no way I could have found it by myself.

 

Thanks again !

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 ,
Jan 27, 2022 Jan 27, 2022

Copy link to clipboard

Copied

This one is working also... and should probably be the one to use.

 

\(.+|\G.+?\K,(?=[^()]*\))

 

 

Get an explanation of the code at Regex101:
https://regex101.com/r/KbUaM3/1

 

 

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 ,
Feb 08, 2022 Feb 08, 2022

Copy link to clipboard

Copied

Terrific website. Didn't know it at all. Thanks for the tip 😉

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 ,
Dec 01, 2022 Dec 01, 2022

Copy link to clipboard

Copied

Hi @Jean-Claude Tremblay and thanks again for this beautiful grep query you've given us. It works as a charm and we've been using it a lot!

 

I was wondering whether you (or anyone else, of course) could help me some more about it.
I would like to achieve the same pattern, but with commas between square brackets

I thought it would be easy, just replace the parentheses with the square brackets and I'll be done. I was wrong, obviously. 

The following query selects every comma that is between parentheses. Works fine.

\(.+|\G.+?\K,(?=[^()]*\))

That can be tested on some examples like the following paraphrase:

  • Maison (Garage, etage, box), Appartement (Garage, etage, box), Bungalow (Garage, etage, box)



I would like to achieve the same goal, but with square brackets, like on the following paraphrase:

  • Maison [Garage, etage, box], Appartement [Garage, etage, box], Bungalow [Garage, etage, box]

I've tried different things, even working from the previous version of the query, but couldn't succeed by myself. So here I am again, asking for your input guys

 

Thanks for your time

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 ,
Dec 01, 2022 Dec 01, 2022

Copy link to clipboard

Copied

Do you mean somthing like that?

\[.+|\G.+?\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 Expert ,
Dec 01, 2022 Dec 01, 2022

Copy link to clipboard

Copied

\[.+|\G.+?\K,(?=[^\[\]]*\])

Follow the same logic has for ( )

\(.+|\G.+?\K,(?=[^()]*\))

but for some unknown mysteries, it is not working for [ ]


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 ,
Dec 01, 2022 Dec 01, 2022

Copy link to clipboard

Copied

I suspect it’s a bug in InDesign, since like we can see in the image below from regex101, it perfectly working...

Screen.png

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 ,
Dec 01, 2022 Dec 01, 2022

Copy link to clipboard

Copied

It’s definitely a bug in InDesign since it is also working perfectly in BBEdit too.

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 ,
Dec 01, 2022 Dec 01, 2022

Copy link to clipboard

Copied

It works for me as well in InDesign:

pixxxelschubser_0-1669933114622.png

 

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 ,
Dec 01, 2022 Dec 01, 2022

Copy link to clipboard

Copied

Wrong alert!

I quite InDesign and relaunch it. It’s working fine 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
Contributor ,
Dec 01, 2022 Dec 01, 2022

Copy link to clipboard

Copied

Thanks @Jean-Claude Tremblay , thanks @pixxxelschubser 

You guys are amazing.
Must say it's quite frustrating not to achieve it by myself, but it also means there is obviously some room to improve ^^let's focus on the positive aspect

 

Thanks again ! big hug, cookie and everything ^^

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 ,
Dec 01, 2022 Dec 01, 2022

Copy link to clipboard

Copied

Thanks are due to @Jean-Claude Tremblay. He did the main work. I merely adapted his GREP to the new circumstances.

😉

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 ,
Dec 01, 2022 Dec 01, 2022

Copy link to clipboard

Copied

LATEST

Sure, but I've tried to do the same and thought I failed. With you confirming the right query, I started to wonder why it worked for you and not for me. When JC talked about a bug in ID, I rebooted my computer, read all the replies, and got it working.

So in the end, your input was valuable to me.

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