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.
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
Copy link to clipboard
Copied
Note: \K is supported with InDesign CS6 and higher.
Regards,
Uwe
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.
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?
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]
Copy link to clipboard
Copied
How do you do a GREP on a string?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I am working in CS5, so do you have a different idea?