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

FindGrep(): How do I store the second subset?

Explorer ,
Dec 06, 2016 Dec 06, 2016

Copy link to clipboard

Copied

Hi there.

I have the following GREP:

app.findGrepPreferences.findWhat = "(^[A-z [:punct:]]+)(\\(\\d{1,3}\\:\\d{1,3}(-\\d{1,3})?\\))(\\.~b)";

I want to store the second subset only (ex. "(28:32)").

How can I do that?

I tried combining positive lookahead with positive lookbehind, with no success.

Any help would be appreciated.

TOPICS
Scripting

Views

372

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 07, 2016 Dec 07, 2016

Copy link to clipboard

Copied

A lookbehind should work, but because it's a variable-length one you should use \K, not (?<=...):

"^[A-z [:punct:]]+\\K(\\(\\d{1,3}\\:\\d{1,3}(-\\d{1,3})?\\))(\\.~b)"

No need for parentheses.

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 ,
Dec 07, 2016 Dec 07, 2016

Copy link to clipboard

Copied

Note: \K is supported with InDesign CS6 and higher.

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 ,
Dec 07, 2016 Dec 07, 2016

Copy link to clipboard

Copied

pkahrel wrote:

"No need for parentheses."

A somewhat longer explanation is:

"findGrep" works in InDesign Space, and its results are also still in InDesign Space. The returned GREP groups, therefore, are also only defined in InDesign Space. And findGrep only returns one kind of result: an array of all 'found items', not split into the orginal groups.¹ It's as if you can only use the "$0" part of the found string.

The only other way I can think of (apart from "make sure to only look for what you want to have returned") is applying a second JavaScript GREP on the returned result, but there are lots of smaller and larger differences between ID's and JS's GREP dialects, so that just may not work.

By the way, you know that "[A-z]" does not return just all uppercase and lowercase characters, right?

¹This'd be a great feature request, though. I have some uses for that.

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
Explorer ,
Dec 07, 2016 Dec 07, 2016

Copy link to clipboard

Copied

"By the way, you know that "[A-z]" does not return just all uppercase and lowercase characters, right?"

What do you mean?

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 07, 2016 Dec 07, 2016

Copy link to clipboard

Copied

[A-z] matches the underline character, too. To match just the plain upper- and lower-case letters, use [A-Za-z]

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
Explorer ,
Dec 07, 2016 Dec 07, 2016

Copy link to clipboard

Copied

How do you do a GREP on a string?

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 07, 2016 Dec 07, 2016

Copy link to clipboard

Copied

LATEST

You do your InDesign "(^[A-z [:punct:]]+)(\\(\\d{1,3}\\:\\d{1,3}(-\\d{1,3})?\\))(\\.~b)", then on each found string, e.g. found.contents, you do a JavaScript GREP. But as JongWare mentioned, JavaScript's GREp is not the same (quite a bit less powerful in fact) tahn InDesign's. Anyway you'd do something like

found = myDocument.findGrep();

for (i = 0; i < found.length; i++) {

  myString = found.contents;

  myNewString = myString.match (/(\(\d{1,3}\:\d{1,3}(-\d{1,3})?\))(\.[\n\r])/); // guessing...

  // and whatever else

}

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
Explorer ,
Dec 07, 2016 Dec 07, 2016

Copy link to clipboard

Copied

I am working in CS5, so do you have a different idea?

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