Skip to main content
Inspiring
December 11, 2023
Answered

Grep matching consecutive letters in a list of words

  • December 11, 2023
  • 3 replies
  • 683 views

 

across

act

action

active

activist

actor

actress

altricial

altruism
altitude

I need help to know if building this grep is possible. The idea is to identify those words with the 4 (may be 5 or 6) first common letters within an alphabetical list.

I tried something like
       [a-z]{4,}|[A-Z]{4,}|\d{4,}

However, it is  almost absurd and I find it very complex to achieve.

Thanks.

 

This topic has been closed for replies.
Correct answer FRIdNGE

… Sure!  😉

 

(^/)  The Jedi

 

/*
    _FRIdNGE-0745_BeginningOfPara.jsx
    Script written by FRIdNGE, Michel Allio [11/12/2023]
*/

app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT, "Beginning Of Para! …");

function main()     
    {
        
        // Name of your Char Style
        var myCharStyle = "red";

        // Place the cursor inside your Story
        var myStory = app.selection[0].parentStory;

        app.findGrepPreferences = app.changeGrepPreferences = null;
        app.findGrepPreferences.findWhat = "(^.{4,})(?=.*\\r\\1)";
        myFound1 = myStory.findGrep();
        for ( var i = 0; i < myFound1.length; i++ ) {
            app.findGrepPreferences = app.changeGrepPreferences = null;
            app.findGrepPreferences.findWhat = "^" + myFound1[i].contents;
            myFound2 = myStory.findGrep();
            for ( var j = 0; j < myFound2.length; j++ ) if ( myFound2[j].characters[0].appliedCharacterStyle.name != myCharStyle ) myFound2[j].appliedCharacterStyle = myCharStyle;
        }
        app.findGrepPreferences = app.changeGrepPreferences = null;

        alert( "Done! …" )

    }

 

3 replies

Peter Kahrel
Community Expert
Community Expert
December 11, 2023

This Grep expression matches consecutive words with the first 4-6 letters in common:

^([\u\l]{4,6}).+\r(\1.+\r)+

To clarify:

^ stands for beginning of paragraph;

(

   [\u\l] stands for upper- or lower-case letter; 

   {4,6} restricts the match to between 4 and 6 letters;

.+\r To the end of paragraph

(

   \1 must match the content of the previous ( )

   then to the end of the paragraph

)

+ more than once

 

Inspiring
December 11, 2023

Yes, fine. It is working.
Very nice replies.

And if It is possible to insert the accented vowels (áéíóú)?
May be in another grep and later we can mix the outputs.

Thanks

Inspiring
December 11, 2023

Jedi, yes. The script was perfect. I copied it again. Thanks.

Mr Kahrel, your grep extended the search for 6 letters. I will use it in Jedi's script. Thanks for the aclaration. Both greps are fine.

 

Inspiring
December 11, 2023

Jedi, mercie for this superb script. Please, help us in two points.
First, this warning message:





Our idea is also to work in Spanish where the point is more interesting. Sabia is an adjective (Wise) and at the same time sabía is the past tense of a verb. Canto is a song and the first person for the verb cantar.


We would like to know if it is feasible to include these words (with accents).

Thanks!

Palala

 

FRIdNGE
December 11, 2023

No error for me!

 

(^/)

 

m1b
Community Expert
Community Expert
December 11, 2023

Hi @palala fog how do you intend to "identify" those characters? A character style?

 

There are some folk on this forum who are incredible with Grep, so they may have Grep style solution, but my gut tells me that for this case a script might be a good way to go.

- Mark

FRIdNGE
FRIdNGECorrect answer
December 11, 2023

… Sure!  😉

 

(^/)  The Jedi

 

/*
    _FRIdNGE-0745_BeginningOfPara.jsx
    Script written by FRIdNGE, Michel Allio [11/12/2023]
*/

app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT, "Beginning Of Para! …");

function main()     
    {
        
        // Name of your Char Style
        var myCharStyle = "red";

        // Place the cursor inside your Story
        var myStory = app.selection[0].parentStory;

        app.findGrepPreferences = app.changeGrepPreferences = null;
        app.findGrepPreferences.findWhat = "(^.{4,})(?=.*\\r\\1)";
        myFound1 = myStory.findGrep();
        for ( var i = 0; i < myFound1.length; i++ ) {
            app.findGrepPreferences = app.changeGrepPreferences = null;
            app.findGrepPreferences.findWhat = "^" + myFound1[i].contents;
            myFound2 = myStory.findGrep();
            for ( var j = 0; j < myFound2.length; j++ ) if ( myFound2[j].characters[0].appliedCharacterStyle.name != myCharStyle ) myFound2[j].appliedCharacterStyle = myCharStyle;
        }
        app.findGrepPreferences = app.changeGrepPreferences = null;

        alert( "Done! …" )

    }