Search for a string having number and increment the found number

Search for a string having number and increment the found number

5
NewbieNewbie
5

    Aug 30, 2016#1

    Hi All,

    I want to make a script which will search for a sting having numbers and increment the found number.

    Say, below is the text in the opened file

    Code: Select all

    #define APP_VER       "10.0.55"
    #define APP_NAME 	"[AddonName]"
    #define APP_TITLE 	"[AddonName]"
    I can search for the "APP_VER" and also get the number "10.0.55", but can not find any Perl script or function to increment .55 to .56.

    Please advise.

    Regards,

    Anand

    6,603547
    Grand MasterGrand Master
    6,603547

      Aug 30, 2016#2

      Here is the comment code for this UltraEdit script:

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      
         // Remember current position of caret in active file.
         var nLine = UltraEdit.activeDocument.currentLineNum;
         var nColumn = UltraEdit.activeDocument.currentColumnNum;
      
         // Move caret to top of the active file.
         UltraEdit.activeDocument.top();
      
         UltraEdit.ueReOn();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=true;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         UltraEdit.activeDocument.findReplace.searchInColumn=false;
      
         // Search case-sensitive with a simple UltraEdit regular expression for
         // the line containing the definition of the application version number.
         if (UltraEdit.activeDocument.findReplace.find('#define +APP_VER[^t ]+"[0-9.]+"'))
         {
            // Cancel the selection by moving caret one character to right.
            UltraEdit.activeDocument.key("RIGHT ARROW");
      
            // Select with another UltraEdit regular expression search in
            // upwards direction just the last number of the version number.
            UltraEdit.activeDocument.findReplace.searchDown=false;
            UltraEdit.activeDocument.findReplace.find('[0-9]+');
      
            // Convert the selected number from string to integer using decimal system.
            var nVersionNumber = parseInt(UltraEdit.activeDocument.selection,10);
      
            // Increment the version number by 1.
            nVersionNumber++;
      
            // Write the incremented number converted back from integer to string
            // into the file with overwriting the still selected version number.
            UltraEdit.activeDocument.write(nVersionNumber.toString(10));
         }
      
         // Restore initial position of caret in active file.
         UltraEdit.activeDocument.gotoLine(nLine,nColumn);
      }
      
      Best regards from an UC/UE/UES for Windows user from Austria

      5
      NewbieNewbie
      5

        Aug 31, 2016#3

        Hi Mofi,

        Thanks a lot for the script. It is working perfectly.  :D
        parseInt() was the function I needed.

        In some of my codes I have version number like below:

        Code: Select all

        #define APP_VER         "10.0.449 Beta"
        Since "Beta" is added to it, the script needed some modification. I added the code for it and also added output window message so that I know it has done the job. Below is the final script.

        Code: Select all

        if (UltraEdit.document.length > 0)  // Is any file opened?
        {
           UltraEdit.outputWindow.showWindow(true);
        
           // Define environment for this script.
           UltraEdit.insertMode();
           if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
           else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
        
           // Remember current position of caret in active file.
           var nLine = UltraEdit.activeDocument.currentLineNum;
           var nColumn = UltraEdit.activeDocument.currentColumnNum;
        
           // Move caret to top of the active file.
           UltraEdit.activeDocument.top();
        
           UltraEdit.ueReOn();
           UltraEdit.activeDocument.findReplace.mode=0;
           UltraEdit.activeDocument.findReplace.matchCase=true;
           UltraEdit.activeDocument.findReplace.matchWord=false;
           UltraEdit.activeDocument.findReplace.regExp=true;
           UltraEdit.activeDocument.findReplace.searchDown=true;
           UltraEdit.activeDocument.findReplace.searchInColumn=false;
        
           // Search case-sensitive with a simple UltraEdit regular expression for
           // the line containing the definition of the application version number.
           if (UltraEdit.activeDocument.findReplace.find('#define +APP_VER[^t ]+"[0-9.. A-Za-z]+"'))
           {
              // Cancel the selection by moving caret one character to right.
              UltraEdit.activeDocument.key("RIGHT ARROW");
        
              // Select with another UltraEdit regular expression search in
              // upwards direction just the last number of the version number.
              UltraEdit.activeDocument.findReplace.searchDown=false;
              UltraEdit.activeDocument.findReplace.find('[0-9]+');
        
              // Convert the selected number from string to integer using decimal system.
              var nVersionNumber = parseInt(UltraEdit.activeDocument.selection,10);
          UltraEdit.outputWindow.write("Old number "+nVersionNumber.toString());
        
              // Increment the version number by 1.
              nVersionNumber++;
        
              // Write the incremented number converted back from integer to string
              // into the file with overwriting the still selected version number.
              UltraEdit.activeDocument.write(nVersionNumber.toString(10));
          UltraEdit.outputWindow.write("New number "+nVersionNumber.toString());
           }
        
           // Restore initial position of caret in active file.
           UltraEdit.activeDocument.gotoLine(nLine,nColumn);
        }
        
        I will try to upload it to the script section, where I uploaded "BoxBracketToComments.js", for the benefit of UltraEdit users.

        Thanks again for your help.

        Regards,

        Anand

        6,603547
        Grand MasterGrand Master
        6,603547

          Aug 31, 2016#4

          Better than "[0-9.. A-Za-z]+" which contains unnecessary the point twice would be "[0-9.]+*" as the asterisk in UltraEdit regular expression syntax means 0 or more characters except newline characters.
          Best regards from an UC/UE/UES for Windows user from Austria

          5
          NewbieNewbie
          5

            Aug 31, 2016#5

            Thanks, it did the trick. Also search is shorter now.

            Regards,

            Anand