9 Replies Latest reply on May 20, 2016 6:04 AM by Andreas Jansson

    How to retreive groups from findGrep()

    hirsute

      Hi,

       

      Having something like this :

      1.1     some text to copy with its style from here to another place

      1.2     more text to copy with its style from here to another place

      1.3     idem

       

      Using that pattern : (^[1-9]+\\.[0-9]+)\\t(.+) I can match each line with findGrep. But now, let say I want to put the first group (^[1-9]+\\.[0-9]+) in a variable and copy the second group (.+) in some other place. The findGrep() method returns an Array of Text. How can I get the content of these groups?

       

      Thanks a lot

        • 1. Re: How to retreive groups from findGrep()
          Peter Kahrel Adobe Community Professional & MVP

          As you say, findGrep() returns an array -- but not of text(s), but of text objects. Say you executed findGrep() using this line:

           

          var found = app.documents[0].findGrep();

           

          then you address the lines as follows:

           

          found[0].contents
          found[1].contents

           

          etc.

           

          Peter

          • 2. Re: How to retreive groups from findGrep()
            [Jongware] Most Valuable Participant

            Peter's is the first half of the solution.

             

            Each returned text object contains the entire found text string -- it's no longer separated into "grep-groups". For this, you need to do a Javascript grep on the found text itself:

             

            finalResult = found[0].contents.match (/(^[1-9]+\\.[0-9]+)\\t(.+)/);

             

            -- finalResult will hold an array of character strings, the first with the entire string, next elements contain each separate group result. (From memory--test, first.)

             

            You will have to take into consideration that Javascript's GREP is a different implementation than the one used in InDesign. Special InDesign codes, such as "~<" for a thin space, will *not* work, and the behavior of a few codes is different.

             

            Besides, since you are manipulating the text property contents (which is a simple text string), you will loose all references to the real formatted text objects in InDesign. If you need this -- because, for example, you want to copy it somewhere else in your document --, you are better off using two separate GREP find commands, using the first to grab only the numbers, and the second for just the remaining text.

            1 person found this helpful
            • 3. Re: How to retreive groups from findGrep()
              Peter Kahrel Adobe Community Professional & MVP

              Theun,

               

              The way the Hairy One formulated his GREP ensures that each element in the found array is precisely one paragraph. So I think that found[0].contents  contains the same as what you would get with findResult = . . .

               

              Peter

              • 4. Re: How to retreive groups from findGrep()
                [Jongware] Most Valuable Participant

                Sure, but let's not forget

                 

                let say I want to put the first group (^[1-9]+\\.[0-9]+) in a variable and copy the second group (.+) in some other place.

                 

                Using the Javascript operator on each of the found results yields access to each separate group.

                • 5. Re: How to retreive groups from findGrep()
                  hirsute Level 1

                  Thank you. I'm trying now to get it by doing separate grep commands as you suggested. Here is the snippet:

                   

                   

                  var myPattern = "(^[1-9]+\\.[0-9]*)\\t(.*)";
                  var myNumPattern = "^[1-9]+\\.[0-9]*";
                  var myEqPattern = "(?=^[1-9]+\\.[0-9]*\\t).*";
                  var myEq;
                  app.findGrepPreferences.findWhat = myPattern;
                  //finds all the matching lines
                  var myFoundItems = myDocument.findGrep();
                  for(var myCounter = 0; myCounter<myFoundItems.length; myCounter++){
                       var myText = myFoundItems[myCounter];
                  //find the number in the matching Text object
                       app.findGrepPreferences.findWhat = myNumPattern;
                       var myNum = myText.findGrep();
                  //find the number in the matching Text object
                       app.findGrepPreferences.findWhat = myEqPattern;
                       myEq = myText.findGrep();
                       //do something with the results
                  

                   

                  It finds the line. But the second and third findGrep return "undefined", even if I try the second and third findGrep with a pattern as simple as (.). How cans I find something within the Text object?

                   

                  Thanks

                  • 6. Re: How to retreive groups from findGrep()
                    [Jongware] Most Valuable Participant

                    There must be something else wrong -- your script totally works for me!

                    (That is, after I changed the five spaces in your sample text to a single tab ...)

                     

                    There is one thing you ought to change:

                    var myEqPattern = "(?=^[1-9]+\\.[0-9]*\\t).*";

                     

                    This will again return the entire string, not just the part after the tab. The lookahead will always match (because that was your initial search criterium), and the text inside the lookahead is then picked up because of ".*". What you need here is a lookbehind ... but that does not work with variable lengths (the '+'es inside the lookbehind).

                    But that's not even necessary. To separate at the first tab, you can simply use this

                     

                    var myEqPattern = "(?<=\t).*";

                     

                    because only text strings that contain a tab somewhere at the start will be searched at this point in the script.

                    • 7. Re: How to retreive groups from findGrep()
                      [Jongware] Most Valuable Participant

                      (Just to make sure: this is the script that works for me. Only changed the top and added the alert at the bottom.)

                       

                      myDocument = app.activeDocument;
                      var myPattern = "(^[1-9]+\\.[0-9]*)\\t(.*)";
                      var myNumPattern = "^[1-9]+\\.[0-9]*";
                      var myEqPattern = "(?<=\t).*";
                      var myEq;
                      app.findGrepPreferences.findWhat = myPattern;
                      //finds all the matching lines
                      var myFoundItems = myDocument.findGrep();
                      for(var myCounter = 0; myCounter<myFoundItems.length; myCounter++)
                      {
                           var myText = myFoundItems[myCounter];
                      //find the number in the matching Text object
                           app.findGrepPreferences.findWhat = myNumPattern;
                           var myNum = myText.findGrep();
                      //find the number in the matching Text object
                           app.findGrepPreferences.findWhat = myEqPattern;
                           myEq = myText.findGrep();
                           //do something with the results
                           alert ('myText: "'+myText.contents+'"\nmyNum:"'+myNum[0].contents+'"\nmyEq: "'+myEq[0].contents+'"');
                      }
                       
                      

                      • 8. Re: How to retreive groups from findGrep()
                        hirsute Level 1

                        Thank you! It works fine now.

                        • 9. Re: How to retreive groups from findGrep()
                          Andreas Jansson Level 2

                          I know this is an old question that has an accepted answer, but still – as I found this thread when looking for a similar thing – I'd like to add that there is a way to move around groups. If you use changeGrep instead of findGrep, you can address the groups using $1, $2 etc, just as in the Find/Change dialogue.

                           

                          The following illustrates in a simplified way how to switch the order of groups 1 and 2.

                           

                          app.findGrepPreferences.findWhat = '(abc)(def)';
                          app.changeGrepPreferences.changeTo = '$2$1'
                          myDocument.changeGrep(reverseOrder)
                          

                           

                          I'm aware that this is somewhat beside the point since you explicitly asked about putting the groups in a variable – which you might need, and also this way won't let you move the groups outside the context in which they were found. But at least this is a way to manipulate the location of groups.