10 Replies Latest reply on Apr 4, 2016 4:39 AM by Laubender

    How Can find numbers and make it plus 34

    karthiks94217840 Level 2

      Hi, Dear friends

       

      I want find a bunch of numbers in my slection and then make it + 34

       

      for Example: pp. & p. \d\d\d

                          I want to make it + 34

       

      Screen Shot 2016-04-04 at 11.17.29 AM.png

       

      Below my code

       

      var _selection = app.selection[0];  
      app.findGrepPreferences = changeGrepPreferences = null;  
      app.findGrepPreferences.findWhat = "p. \d\d\d";  
      var found = app.selection[0].findGrep();  
      for(var i=0;i<found.length;i++)  
      {  
              app.findGrepPreferences = changeGrepPreferences = null;  
              app.findGrepPreferences.findWhat = "p. \d\d\d";  
              var foundnum = found[i].findGrep();  
              var newnum = parseInt(foundnum[0].contents) + 34;  
              app.changeGrepPreferences.changeTo = newnum.toString();  
              found[i].changeGrep();  
          }  
      alert ("Done")
      

       

      I think my formula is wrong? Please advice friend.

       

      Advance Thanks.

        • 1. Re: How Can find numbers and make it plus 34
          b91823603 Level 3

          Hi

           

          Modify the '\' in regex with '\\' as below, Try the code,

           

          var _selection = app.selection[0];  

          app.findGrepPreferences = changeGrepPreferences = null;  

          app.findGrepPreferences.findWhat = "p. \\d\\d\\d";  

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

          for(var i=0;i<found.length;i++)  

          {  

                  app.findGrepPreferences = changeGrepPreferences = null;  

                  app.findGrepPreferences.findWhat = "\\d\\d\\d";  

                  var foundnum = found[i].findGrep();  

                  var newnum = parseInt(foundnum[0].contents) + 34;  

                  app.changeGrepPreferences.changeTo = newnum.toString();  

                  found[i].changeGrep();  

              }  

          alert ("Done")

          • 2. Re: How Can find numbers and make it plus 34
            karthiks94217840 Level 2

            HI b91823603

             

            Thanks so match B. your code is working super .

             

            One question: But, i have trying this formula "p. \\d\\d\\d" for indesign file ("Find/Change" Grep option). Since, that formula not working. Why? please advice friend.

             

            Advance Thanks

            • 3. Re: How Can find numbers and make it plus 34
              Harbs. Level 6

              In scripts, you need double backslashes because Javascript will lose a single backslash. If you are writing the GREP in InDesign, it would be a single backslash.

              • 4. Re: How Can find numbers and make it plus 34
                b91823603 Level 3

                Hi,

                 

                If you use '\d' in your script regex it takes only 'd' in In-design Find/Change option.

                 

                you need to use '\\d' in script to find the '\d' in In-design Find/Change option.

                 

                For your reference run the above code, And then check the Find/Change Grep option's Find What value in In-design. Then Check with '\' also...

                • 5. Re: How Can find numbers and make it plus 34
                  karthiks94217840 Level 2

                  HI b91823603

                   

                  Okay, Thanks.

                   

                  Thanks

                  • 6. Re: How Can find numbers and make it plus 34
                    Laubender Adobe Community Professional & MVP

                    Hi Karthi,

                    I think, your GREP perhaps will find too much.

                    Of course that is depending on your actual text.

                     

                    Why not searching for a number and using a postive lookbehind and a positive lookahead if you only want to operate on three-digit numbers, that are enclosed in brackets?

                    Also escape your dot, if you actually want to find a dot and not an arbitrary character.

                     

                    The following GREP will do this (InDesign CS6 and above) by using the flexible lookbehind \K, that was silently introduced in CS6:

                     

                    var myGrep = "(\\(p{1,2}\\. )\\K\\d{3}(?=\\))";
                    
                    
                    
                    
                    

                     

                    Then you need only to operate on the found number.

                    And if there is a text like that: "(pp. 1001)" currently you will add 34 to the first three digits of that number like that: "(pp. 1341)".

                     

                    This code will exactly find a three digit number, that is preceded by a opening bracket "(" plus one or two "p" plus one dot plus a blank and followed by a bracket.

                     

                    var amountToAdd = 34;
                    var _selection = app.selection[0];
                    
                    /*
                        Find a three digit number
                        if "(p. " or "(pp. ") is preceding and a closing bracket ")" is following that.
                    
                        Used
                        a flexible positive lookbehind with \K  (InDesgn CS6 and above)
                        a positive lookahead with a bracket (?=\))
                    
                        Escape the escapes in the string.
                        Escape the dot in the string, if you actually mean a dot and not an arbitrary character.
                    
                        The positive lookahead is important
                        to restrict the search.
                    
                    */
                    
                    var myGrep = "(\\(p{1,2}\\. )\\K\\d{3}(?=\\))";
                    
                    app.findGrepPreferences = changeGrepPreferences = null;
                    app.findGrepPreferences.findWhat = myGrep;
                    
                    // Search from back to force in the text:
                    var found = app.selection[0].findGrep(true);
                    
                    for(var n=0;n<found.length;n++)
                    {
                        // Assigning new contents to found number:
                        found[n].contents = ( parseInt(found[n].contents) + amountToAdd ).toString();
                    };
                    
                    
                    
                    
                    

                     

                     

                    // EDIT: Had to edit the GREP after posting, because I forgot to escape one escape.

                    // EDIT 2: I tested for one to two digit numbers first and left that in the code snippet \\d{1,3}; changed to three digits only now: \\d{3}

                     

                    Uwe

                    • 7. Re: How Can find numbers and make it plus 34
                      karthiks94217840 Level 2

                      HI Laubender

                       

                      First Thanks for your explain (formula "\K") also posting script. your script is working good.

                       

                      One question: That script some of the number is not changed.

                       

                      Example: "pp. 230, 240" value.

                       

                      Thanks

                      • 8. Re: How Can find numbers and make it plus 34
                        Laubender Adobe Community Professional & MVP

                        Yes. Because that pattern does not match the text. See the positive lookahead.

                        For occassions like "pp. 230, 240" we need a different GREP.

                         

                        About finding too much:

                        Your original GREP will also find the numbers in the following text:

                         

                        "I was looking for an app. 1000 miles away from home.

                        And there was that gap: 101 inches wide."

                         

                        And will transform it to:

                         

                        "I was looking for an app. 1340 miles away from home.

                        And there was that gap: 135 inches wide."

                         

                        For testing GREP patterns I recommend the scripts by Peter Kahrel here:

                        GREP utilities | Peter Kahrel

                         

                         

                        Uwe

                        • 9. Re: How Can find numbers and make it plus 34
                          karthiks94217840 Level 2

                          HI Laubender

                           

                          Your teaching is very very well(super) friend.

                           

                          I will try to based on the your above link than get back to you Launder.

                           

                          One more thanks

                           

                          Thanks

                          • 10. Re: How Can find numbers and make it plus 34
                            Laubender Adobe Community Professional & MVP

                            For occassions like "pp. 230, 240" we need a different GREP.

                             

                            For the second search/replace action you now have to do a lookbehind for the first part of the expression "(pp. 230, " that will not be changed. Plus the familiar pattern "\\d{3}" for the second number that will be changed and the same positive lookahead as before. Provided all your text you are looking for is in brackets. Your last sample is showing no brackets. What I cannot know is, if there are text samples like that:

                             

                            "(pp. 230, 240, 250 and 270)"

                             

                            That would complicate the case even more.

                            Then I would recommend a different strategy.

                            It all depends on your text.

                             

                            First know your text, then do the search pattern and the replace strategy.
                            And if you are asking for patterns in the future here, provide a variety of good samples before.

                             

                            Note: Three or more digit numbers could be expressed as:

                            "\\d{3,}"
                            

                             

                            But also this would fail, if your sample text is this like:

                            "(pp. 1,980, 1,985, 1,990 and 1,999)"

                             

                            Searching for three or more digit numbers and adding 34 would lead to:

                            "(pp. 1,1014, 1,1019, 1,1024 and 1,1033)"

                             

                            Uwe