6 Replies Latest reply on Aug 18, 2014 10:20 AM by pixxxel schubser

    Modifying findGrep results when using lookaheads/lookbehinds [JS][CS6]

    HH_BFS Level 1

      Dear all,

       

      I am thinking about a search/replace script (JS) in which the user is able to choose in a GUI to which elements of a findGrep()-array a changeGrep() method should be applied to. All in all that should be an easy thing to do.

       

      The only thing I am wondering about is dealing with lookaheads and lookbehinds. Assuming the findWhat expression to be " (?=!)" to find a single unwanted space in front of an exclamation mark. Please understand this just as a simple example, it is obvious that you can do that kind of simple search without using a lookahead. The findGrep array will contain the pointer to the found space. If the user decides to remove the space and applies the changeGrep method on it, no changes will be made because the exclamation mark is not stored in the findGrep array and hence changeGrep method will not match the space.

       

      So I am thinking of extending the pointer/selection before using changeGrep to include the exclamation mark in order to successfully apply changeGrep. But maybe there is a more straight solution I am not aware of?

       

      Would be glad to read your point of view here.

       

      Thank you very much!

       

      Jan

        • 1. Re: Modifying findGrep results when using lookaheads/lookbehinds [JS][CS6]
          Jump_Over Level 5

          Hi,

           

          I am not sure if kept follow your example but something tells me you can use matching groups to reach a goal.

          I mean find (\s)(!) and change to $2 keeping possibility to apply more properties to "!"

           

          Jarek

          • 2. Re: Modifying findGrep results when using lookaheads/lookbehinds [JS][CS6]
            HH_BFS Level 1

            Hi Jarek,

             

            yes, thank you! You are totally right, also in my opinion matching groups should be the proper way to clean up the text in the given example.

             

            But I have to admit that my example was by far not the best to illustrate the demand of a lookahead/lookbehind and it was not my intention to analyze this specific find expression. Assuming there are some cases in which a lookahead/lookbehind might be the only way to go, my question was if there is an working approach dealing with it, when only specific places of finding should by edited by changeGrep method.

             

            Nevertheless many thanks for your reply and sorry for my mistakable question.

             

            Jan

            • 3. Re: Modifying findGrep results when using lookaheads/lookbehinds [JS][CS6]
              pixxxel schubser Level 5

              HH_BFS wrote:

               

              … If the user decides to remove the space and applies the changeGrep method on it, no changes will be made because the exclamation mark is not stored in the findGrep array and hence changeGrep method will not match the space …

               

              Hi @HH_BFS,

              I'm a little bit confused.

               

              Why should this Grep not works?

              Do you use the right syntax in your script?

              app.findGrepPreferences = app.changeGrepPreferences = null;
              app.findGrepPreferences.findWhat = ' (?=!)';
              app.changeGrepPreferences.changeTo = '';
              app.changeGrep();
              app.findGrepPreferences = app.changeGrepPreferences = null;
              
              

               

              This will always do the job.

               

              And yes, you have always several posibilities with Grep (please ignore the double quotation marks).

              " (?=!)" replace with nothing is getting the same result as

              "( )(!)" and replace with $2 is getting the same result as

              "( !)" and replace with "!" is getting the same result as

              " !" and replace with "!"

               

              Have fun

               

              • 4. Re: Re: Modifying findGrep results when using lookaheads/lookbehinds [JS][CS6]
                HH_BFS Level 1

                Hey pixxxel schubser,

                 

                many thanks for your reply! I think I am causing confusion when not showing any code. The script still is an idea, so there is nothing to present. But maybe these lines will illustrate my problem. And to underline it, please let me tell again, that my question adresses doing specific changes when using lookaheads/lookbehinds in general. The given bloody example with space and exclamation mark is really just an simple illustration.

                 

                //somewhere is the location to search at
                app.findGrepPreferences = app.changeGrepPreferences = null;
                app.findGrepPreferences.findWhat = " (?=!)";
                app.findGrepPreferences.changeTo = "";
                //contains all found content as an array
                var found = somewhere.findGrep ();
                
                
                
                
                //now again imagination is required ... here i would like to list all found content in a gui (listbox i.e.). so i would build up the gui window, loop through the "found" array and present the result. that is not a problem ...
                for (var i = 0; i < found.length; i++) {
                
                //imagination ...
                
                
                }
                
                //user clicks specific entry in gui, so that i.e. only found[0] should get corrected (and not simply all entries). something like:
                found[0].changeGrep ();
                
                //now (in my opinion) the problem:
                //but found[0] only contents " " (space). so the changeGrep method will not match, because the "!" is missing. Nothing changes.
                //it is clear to me that this happens because the findGrep method finds matching content in which the changeGrep tries a second find with replace. that can not work well.
                //so i would be happy, if i could just give up the findGrep method and tell the changeGrep method only to change i.e. the first (or second ... or whatever) match. But I think that is not possible.
                
                
                app.findGrepPreferences = app.changeGrepPreferences = null;
                
                //and so on ...
                
                

                 

                Maybe that clarifies things.

                 

                Thank you,

                BR

                 

                Jan

                • 5. Re: Modifying findGrep results when using lookaheads/lookbehinds [JS][CS6]
                  Jump_Over Level 5

                  Hi,

                   

                  Now I am confused a little...

                   

                  In above case you should simply do change instead of involving method changeGrep()

                  cause it is another part of text playing with.

                  I mean:

                       found[i].remove();

                  or

                       found[i].contents = app.changeGrepPreferences.changeTo;

                   

                  Jarek

                  • 6. Re: Re: Re: Modifying findGrep results when using lookaheads/lookbehinds [JS][CS6]
                    pixxxel schubser Level 5

                    HH_BFS wrote:

                     

                    Hey pixxxel schubser,

                     

                    many thanks for your reply! I think I am causing confusion when not showing any code. The script still is an idea, so there is nothing to present. But maybe these lines will illustrate my problem. And to underline it, please let me tell again, that my question adresses doing specific changes when using lookaheads/lookbehinds in general. The given bloody example with space and exclamation mark is really just an simple illustration.

                     

                    1.  
                    2. //now (in my opinion) the problem: 
                    3. //but found[0] only contents " " (space). so the changeGrep method will not match, because the "!" is missing. Nothing changes. 
                    4. //it is clear to me that this happens because the findGrep method finds matching content in which the changeGrep tries a second find with replace. that can not work well. 
                    5. //so i would be happy, if i could just give up the findGrep method and tell the changeGrep method only to change i.e. the first (or second ... or whatever) match. But I think that is not possible. 
                    6.  

                     

                    A little suggestion:

                    // required: opened document with one text frame
                    // with contents e.g. The search for the "h" in the middle of the first "the"
                    // and show text before and behind (in Javascript-Console)
                    var aDoc = app.activeDocument;
                    var aTF = aDoc.textFrames[0];
                    
                    var found = aTF.contents.match(/(\w+ )(t)(h)(e)( \w+)/);
                    $.writeln(found);
                    
                    $.writeln(RegExp.$1 + "  |  " + RegExp.$2 + "  |  " + RegExp.$3 + "  |  " + RegExp.$4 + "  |  " + RegExp.$5 );
                    

                     

                     

                    Try it out. This will be sometimes complicated for the programmer (I mean you ), but perhaps one possible way to go.

                     

                    Have fun