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

How to combine shortest match with location, using GREP styles?

New Here ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

HI

This question is probably easy for those more experienced, but I'm having problem to make it work. I want to apply a GREP style to some text between parentheses, but not to all of them, only in a certain location (in my case, just when the parentheses are at the end of the paragraph).

I started with this, and it worked well, selecting all texts between parentheses:

grep_paragrafo_1.jpg

Then, I thought that inserting a $ at the end of my expression, it would  select just the cases when the parentheses ocurred at the end of the paragraph. But it didn't work, and the selection came back to the "greedy" mode, as seen in the picture:

grep_paragrafo_2.jpg

It worked in the second paragraph, in which we have just on occasion of parentheses, and they are at the end. But in the first paragraph, I want the GREP to be applied just  in (nsdvnsvk). How can I have the expected result?

Thanks,

TOPICS
Scripting

Views

2.6K

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 1 Correct answer

Community Expert , Jan 16, 2017 Jan 16, 2017

Ah, yes, that one. I described it even in the PDF, p. 53:

\([^)]+\)$

\(      match an opening parenthesis

[^)]+   continue matching while not a closing parenthesis

\)       then match that parenthesis . . .

$       . . . at the end of the paragraph.

Peter

Votes

Translate

Translate
Community Expert ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

\(.+?\)$

should do it.

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
New Here ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Hi Peter

Have you tested it? Because that's exactly what I did, but for me it doesn't work. It applies from the first opening parentheses in the paragraph to the last closing parentheses right before the end of the paragraph, instead of appplying just to the shortest match before the end of the paragraph…

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 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Ah, yes, that one. I described it even in the PDF, p. 53:

\([^)]+\)$

\(      match an opening parenthesis

[^)]+   continue matching while not a closing parenthesis

\)       then match that parenthesis . . .

$       . . . at the end of the paragraph.

Peter

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
New Here ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Thank you very much! That's it, now it worked perfectly!

By the way, what PDF are you talking about?

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 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

> By the way, what PDF are you talking about?

This one: GREP in InDesign - O'Reilly Media

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
New Here ,
Jan 17, 2017 Jan 17, 2017

Copy link to clipboard

Copied

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
LEGEND ,
Jan 17, 2017 Jan 17, 2017

Copy link to clipboard

Copied

LATEST

Hi,

I've detected a problem on all our proposals!

This case doesn't work correctly:

Capture d’écran 2017-01-17 à 18.19.10.png

It's not taken in account by  (?>\(.+?\))$  [normal!] but this code has another default (too greedy!).

… there's another approach! It will need 2 codes:

1/  \(([^()]|(?R))*\)  with a grep style associated to a "X" cur style. I use this code to validate ( ), [ ], " " constructions that by default work per pairs.

Capture d’écran 2017-01-17 à 18.23.57.png

2/ A really simple find/replace  .+$  based on this "X" char style.

Capture d’écran 2017-01-17 à 18.29.20.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
LEGEND ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Hi Peter,

(?>\(.+?\))$

(^/) 

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
LEGEND ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Hi Uwe,

(?-s)(.+)?\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 ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Not sure how (?>\(.+?\))$ works, Michel. Could you elaborate?

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
LEGEND ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Peter,

(?> …) is called "atomic group". It's a indivisible entity!

It is totally relevant here. It means here "the shortest occurrence from the para end"!  Cool!

When you gave your first answer, I've validated it too because it seems to us as an evidence!

… But, as the target is the end of the para, I forgot it and we're wrong: the first ( found is not the first one from the end of the para! 

Your 2nd code is totally relevant! … but I like this new code! 

(^/)

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 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Interesting. Didn't know about atomic groups.

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
LEGEND ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

I've tested our 4 grep codes! … with grep styles, grep find/replace … and ID version (mine is from Tatooine, in a galaxy far, far away!).

… The results are … very astonishing!

Don't forget, I use ID CC 2015 (from Tatooine)

Grep Styles:

Peter's code:  \([^(]+\)$ seems better!

Capture d’écran 2017-01-16 à 22.15.41.png

Uwe's code: nothing catched! [I've already discussed about this point with Jean-Claude Tremblay who had no problem and seems to use a similar version as Uwe's one!]

Capture d’écran 2017-01-16 à 22.16.14.png

Ben's code 1:

Capture d’écran 2017-01-16 à 22.16.30.png

Ben's code 2:

Capture d’écran 2017-01-16 à 22.16.49.png

Find/Replace: I've used different color conditions for the 4 codes

Capture d’écran 2017-01-16 à 22.33.18.png

Temporary conclusion:

It seems that, with my ID version, the more relevant code, working in the same way with grep style and find/replace, is:

(?>\(.+?\))$

+ a gift: it avoids a "catch" error!

Catching:

(rectibus maxim vernam exere atia (velliquodis nis reiur?))

… it takes the good parentheses!

May The Force Be With You!

(^/) 

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 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Obi-wan Kenobi wrote:

Hi Peter,

(?>\(.+?\))$

(^/)

Cool!

Here my test with your "atomic group" at the end of a paragraph (or before a line separator sign) and is working as expected:

ParaStyle-3-with-AtomicGroup-in-GREPStyle.png

Regards,
Uwe

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 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

pkahrel wrote:

Ah, yes, that one.

Am I missing something, or should Silvio's attempt actually have worked? Because I've been scratchin' my head over it and from the looks of it, it ought to work.

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 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

It's the $ that throws a spanner in the wheels, I fell for it (again). Without it, \(.+?\) matches every shortest parenthetical. But with the $, the expression tries to match from the first opening parenthesis to the first closing parenthesis that's at the end of the paragraph.

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 17, 2017 Jan 17, 2017

Copy link to clipboard

Copied

Nice use of the Atomic Group Michel! It make my head spin «explode», so I’m more pacific and use one of these three queries that will also works.

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

\([^(]+?\)(?=[\r\n])

\([^(]+?\)(?=\p{Cc})

or this one too in CS6 only:

^.+\K\(.+?\)(?=\p{Cc})

^.+\K\(.+?\)(?=[\r\n])

^.+\K\(.+?\)(?=$)

or removing the ^ like this in CS6 and above:

.+\K\(.+?\)(?=\p{Cc})

.+\K\(.+?\)(?=[\r\n])

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

PS: In CC, a bug seems to have been introduced that make regex with the ^ to fail after the first line break.

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 17, 2017 Jan 17, 2017

Copy link to clipboard

Copied

Jean-Claude,

The lookahead in \([^(]+?\)(?=$) is not necessary because $, the paragraph end, is a position, not a character. And $ works before \n as well, so \([^(]+?\)$ works for both \([^(]+?\)(?=$) and \([^(]+?\)(?=[\r\n])

The last one doesn't work for paragraphs at story/cell end which that don't terminate in a paragraph return, so \([^(]+?\)$ is really the better one.

Peter

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 17, 2017 Jan 17, 2017

Copy link to clipboard

Copied

You’re right Peter, thanks.

And thanks Jongware. Now can someone explain me realllllly slowly how Atomic Group is working?

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 17, 2017 Jan 17, 2017

Copy link to clipboard

Copied

pkahrel wrote:

... with the $, the expression tries to match from the first opening parenthesis to the first closing parenthesis that's at the end of the paragraph.

After someone explained it real slow to me, I finally got it.

Why this happens is that it is not only the shortest match (in fact, the question mark in the ".+?" part is a red herring), but it is the only possible match! The regex always starts at the beginning of the paragraph, and there is only one opening parenthesis which ends with the combination ")$".

Sure, there is another opening parenthesis inside that line – but a regex match always starts at the beginning (!!) and so that one is never considered and it gets silently eaten by the ".+". Since the start condition "(" already has been matched, there is no reason to check for it 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 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Hi Silvio,

could you test the one below for InDesign CS6 ( and versions above)?

^(.+)?\K\(.+?\)(?=\r)

\K is a "postive look behind" with the ability to catch strings of different length.

Regards,
Uwe

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
New Here ,
Jan 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

Hi Uwe

It worked partially. It worked just if it appears in the first parapraph of the story. If there's more than one occurrence (or even if there's just one, but not in the first paragraph), it doesn't work…

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 16, 2017 Jan 16, 2017

Copy link to clipboard

Copied

SilvioGabriel wrote:

Hi Uwe

It worked partially. It worked just if it appears in the first parapraph of the story. If there's more than one occurrence (or even if there's just one, but not in the first paragraph), it doesn't work…

Hi Silvio,

interesting…

I tested with CS6 v8.1.0 on OSX 10.7.5 and it seemed to work on my side.

The whole story is formatted with "ParaStyle-1":

ParaStyle-1-with-GREPStyle.png

What is your version of InDesign on what OS?

Regards,
Uwe

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
New Here ,
Jan 17, 2017 Jan 17, 2017

Copy link to clipboard

Copied

Hi

I'm working with Indesign CC 2017.0 Release, 12.0.0.81 x64, Windows 7 Enterprise.

grep_paragrafo_3.jpg

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