Skip to main content
Inspiring
February 21, 2023
Answered

GREP query

  • February 21, 2023
  • 3 replies
  • 2586 views

I have 2 Find & Replace actions that I wish to achieve with GREP.

 

First, find all sentences beginning with the word "Quest" and replace with a paragraph break.

Second, find all numbers in brackets and replace them with alphabets that follow consecutively from A-Z (and, automatically restart from A once 26 digits have been replaced).
For example,

He was not happy(1) about his situation(2).

So he went(1) to buy icecream(2).

should become

 

He was not happya about his situationb.

So he wentc to buy icecreamd.


I'm currently familiar with setting up character styles, as well as replacing format in the GREP window, so I would add a superscript format to the "change" field in the second equation. 

Looking forward to some great GREP strings.

This topic has been closed for replies.
Correct answer FRIdNGE

Including the "superscript" char style (and removing the parenthesis):

 

/*
    by FRIdNGE, Michel Allio [21/02/2023]
*/

// Simplistically:

var myDoc = app.activeDocument;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "\\(\\d+\\)";
myFound = myDoc.findGrep();
app.changeGrepPreferences.appliedCharacterStyle = "superscript";
myDoc.changeGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;
var F = myFound.length,  f;
for ( var f = 0; f < F; f++ ) {
    if ( f%26 == 0 ) myFound[f].contents = "a";
    if ( f%26 == 1 ) myFound[f].contents = "b";
    if ( f%26 == 2 ) myFound[f].contents = "c";
    if ( f%26 == 3 ) myFound[f].contents = "d";
    if ( f%26 == 4 ) myFound[f].contents = "e";
    if ( f%26 == 5 ) myFound[f].contents = "f";
    if ( f%26 == 6 ) myFound[f].contents = "g";
    if ( f%26 == 7 ) myFound[f].contents = "h";
    if ( f%26 == 8 ) myFound[f].contents = "i";
    if ( f%26 == 9 ) myFound[f].contents = "j";
    if ( f%26 == 10 ) myFound[f].contents = "k";
    if ( f%26 == 11 ) myFound[f].contents = "l";
    if ( f%26 == 12 ) myFound[f].contents = "m";
    if ( f%26 == 13 ) myFound[f].contents = "n";
    if ( f%26 == 14 ) myFound[f].contents = "o";
    if ( f%26 == 15 ) myFound[f].contents = "p";
    if ( f%26 == 16 ) myFound[f].contents = "q";
    if ( f%26 == 17 ) myFound[f].contents = "r";
    if ( f%26 == 18 ) myFound[f].contents = "s";
    if ( f%26 == 19 ) myFound[f].contents = "t";
    if ( f%26 == 20 ) myFound[f].contents = "u";
    if ( f%26 == 21 ) myFound[f].contents = "v";
    if ( f%26 == 22 ) myFound[f].contents = "w";
    if ( f%26 == 23 ) myFound[f].contents = "x";
    if ( f%26 == 24 ) myFound[f].contents = "y";
    if ( f%26 == 25 ) myFound[f].contents = "z";
}
alert( "Youpi! …" )

 

(^/)

3 replies

m1b
Community Expert
Community Expert
February 21, 2023

By the way, @gethmed, a grep for finding paragraph that starts with "Quest" is

^Quest\b.*$

- Mark 

gethmedAuthor
Inspiring
February 22, 2023

Thanks @m1b . This was neat too.

Now I'm wondering if there's a way to not merely find the paragraph, but cut all the paragraphs that start with "Quest".
I found a particular forum here that has a script to cut text - https://creativepro.com/topic/grep-to-cut-text-and-paste-in-separate-text-box/

The script didn't load in Indesign though I replaced the GREP expression in that script.

If there's a script to cut all the paragraphs that fulfil your GREP expression and paste into a new text frame, that'll be great.
But cutting all of it to the clipboard would suffice for me. I can manually paste it into a text frame and apply my desired Character + Para style accordingly.

m1b
Community Expert
Community Expert
March 12, 2023

Hi @gethmed, did you solve this problem? If not, it might be worth asking as a new topic?

- Mark

Peter Spier
Community Expert
Community Expert
February 21, 2023

Your second request will require a script. Find/change has no way to increment.

FRIdNGE
February 21, 2023

True!

 

/*
    by FRIdNGE, Michel Allio [21/02/2023]
*/

// Simplistically:

var myDoc = app.activeDocument;
app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = "\\(\\d+\\)";
myFound = myDoc.findGrep();
var F = myFound.length,  f;
for ( var f = 0; f < F; f++ ) {
    if ( f%26 == 0 ) myFound[f].contents = "(a)";
    if ( f%26 == 1 ) myFound[f].contents = "(b)";
    if ( f%26 == 2 ) myFound[f].contents = "(c)";
    if ( f%26 == 3 ) myFound[f].contents = "(d)";
    if ( f%26 == 4 ) myFound[f].contents = "(e)";
    if ( f%26 == 5 ) myFound[f].contents = "(f)";
    if ( f%26 == 6 ) myFound[f].contents = "(g)";
    if ( f%26 == 7 ) myFound[f].contents = "(h)";
    if ( f%26 == 8 ) myFound[f].contents = "(i)";
    if ( f%26 == 9 ) myFound[f].contents = "(j)";
    if ( f%26 == 10 ) myFound[f].contents = "(k)";
    if ( f%26 == 11 ) myFound[f].contents = "(l)";
    if ( f%26 == 12 ) myFound[f].contents = "(m)";
    if ( f%26 == 13 ) myFound[f].contents = "(n)";
    if ( f%26 == 14 ) myFound[f].contents = "(o)";
    if ( f%26 == 15 ) myFound[f].contents = "(p)";
    if ( f%26 == 16 ) myFound[f].contents = "(q)";
    if ( f%26 == 17 ) myFound[f].contents = "(r)";
    if ( f%26 == 18 ) myFound[f].contents = "(s)";
    if ( f%26 == 19 ) myFound[f].contents = "(t)";
    if ( f%26 == 20 ) myFound[f].contents = "(u)";
    if ( f%26 == 21 ) myFound[f].contents = "(v)";
    if ( f%26 == 22 ) myFound[f].contents = "(w)";
    if ( f%26 == 23 ) myFound[f].contents = "(x)";
    if ( f%26 == 24 ) myFound[f].contents = "(y)";
    if ( f%26 == 25 ) myFound[f].contents = "(z)";
}
app.findGrepPreferences = null;

alert( "Youpi! …" )

 

(^/)

FRIdNGE
FRIdNGECorrect answer
February 21, 2023

Including the "superscript" char style (and removing the parenthesis):

 

/*
    by FRIdNGE, Michel Allio [21/02/2023]
*/

// Simplistically:

var myDoc = app.activeDocument;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "\\(\\d+\\)";
myFound = myDoc.findGrep();
app.changeGrepPreferences.appliedCharacterStyle = "superscript";
myDoc.changeGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;
var F = myFound.length,  f;
for ( var f = 0; f < F; f++ ) {
    if ( f%26 == 0 ) myFound[f].contents = "a";
    if ( f%26 == 1 ) myFound[f].contents = "b";
    if ( f%26 == 2 ) myFound[f].contents = "c";
    if ( f%26 == 3 ) myFound[f].contents = "d";
    if ( f%26 == 4 ) myFound[f].contents = "e";
    if ( f%26 == 5 ) myFound[f].contents = "f";
    if ( f%26 == 6 ) myFound[f].contents = "g";
    if ( f%26 == 7 ) myFound[f].contents = "h";
    if ( f%26 == 8 ) myFound[f].contents = "i";
    if ( f%26 == 9 ) myFound[f].contents = "j";
    if ( f%26 == 10 ) myFound[f].contents = "k";
    if ( f%26 == 11 ) myFound[f].contents = "l";
    if ( f%26 == 12 ) myFound[f].contents = "m";
    if ( f%26 == 13 ) myFound[f].contents = "n";
    if ( f%26 == 14 ) myFound[f].contents = "o";
    if ( f%26 == 15 ) myFound[f].contents = "p";
    if ( f%26 == 16 ) myFound[f].contents = "q";
    if ( f%26 == 17 ) myFound[f].contents = "r";
    if ( f%26 == 18 ) myFound[f].contents = "s";
    if ( f%26 == 19 ) myFound[f].contents = "t";
    if ( f%26 == 20 ) myFound[f].contents = "u";
    if ( f%26 == 21 ) myFound[f].contents = "v";
    if ( f%26 == 22 ) myFound[f].contents = "w";
    if ( f%26 == 23 ) myFound[f].contents = "x";
    if ( f%26 == 24 ) myFound[f].contents = "y";
    if ( f%26 == 25 ) myFound[f].contents = "z";
}
alert( "Youpi! …" )

 

(^/)

Community Expert
February 21, 2023

For your first question, do you want a return inserted before the sentence beginning with Quest?

For your second question, this almost appears as you are working with footnotes, and if so would this task be better suited creating properly styled footnotes instead of the grep search?