Find and replace numbers by increments

Find and replace numbers by increments

12
Basic UserBasic User
12

    Mar 23, 2013#1

    Hello,

    I have UltraEdit 16.30, and I'm trying to find a way to replace numbers by an incremented amount. What I have done is to manually create anchors for notes as follows:

    {n1}
    {n2}
    {n3}

    etc.

    But I have accidentally repeated {n46}, so that the subsequent numbers are out by one, i.e. I need to add "1" to all the following instances. I could of course do it manually, but that is slow and could lead to further error.

    Is there an easy formula that can be employed to do this automatically?

    Just as a matter of interest, and for my future reference, is there also a way to automatically number instances of the same characters?

    If for example I had put just {n} beside all of my notes, and there were a hundred of them - can they automatically be numbered 1-100, like:

    {n1}
    {n2}
    .....
    {n99}
    {n100}

    6,603548
    Grand MasterGrand Master
    6,603548

      Mar 23, 2013#2

      That cannot be done with a single Replace command. You need a script for this task. Examples can be found in the topics

      12
      Basic UserBasic User
      12

        Mar 24, 2013#3

        Thank you for your response.

        The trouble is that I have never used a script before. I changed it to the following to suit my search parameters but got the message that I had entered an invalid regular expression.

        My not working script:

        Code: Select all

        if (UltraEdit.document.length > 0) {
           UltraEdit.insertMode();
           if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
           else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
           UltraEdit.activeDocument.hexOff();
           UltraEdit.perlReOn();
           UltraEdit.activeDocument.top();
           UltraEdit.activeDocument.findReplace.searchDown=true;
           UltraEdit.activeDocument.findReplace.mode=0;
           UltraEdit.activeDocument.findReplace.matchCase=true;
           UltraEdit.activeDocument.findReplace.matchWord=false;
           UltraEdit.activeDocument.findReplace.regExp=true;
           if (UltraEdit.activeDocument.findReplace.find("{n\\d+}")) {
              UltraEdit.activeDocument.endSelect();
              UltraEdit.activeDocument.key("LEFT ARROW");
              UltraEdit.activeDocument.findReplace.searchDown=false;
              UltraEdit.activeDocument.findReplace.find("\\d+")
              var sNumber = UltraEdit.activeDocument.selection;
              var nNumber = parseInt(sNumber,10);
              nNumber++;
              sNumber = nNumber.toString(10);
              UltraEdit.activeDocument.findReplace.searchDown=true;
              UltraEdit.activeDocument.findReplace.replaceAll=true;
              UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
              UltraEdit.activeDocument.findReplace.selectText=false;
              UltraEdit.activeDocument.findReplace.preserveCase=false;
              UltraEdit.activeDocument.findReplace.replace("{n\\d+}", "{n"+sNumber+"}");
           }
        }
        Also, once I created the script I had no idea what file type to save it as, so I saved it as text.

        6,603548
        Grand MasterGrand Master
        6,603548

          Mar 24, 2013#4

          The find string must be "\\{n\\d+\\}" as {...} has a special meaning in Perl regular expressions and therefore { and } must be escaped with a backslash to just find these 2 characters, see the IDM power tip Getting started with Perl regex in UltraEdit and UEStudio.

          However, the script can be made simplier and faster by making the search string a little bit more complicated.

          Code: Select all

          if (UltraEdit.document.length > 0)
          {
             UltraEdit.insertMode();
             if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
             else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
             UltraEdit.activeDocument.hexOff();
             UltraEdit.perlReOn();
             UltraEdit.activeDocument.top();
             UltraEdit.activeDocument.findReplace.searchDown=true;
             UltraEdit.activeDocument.findReplace.mode=0;
             UltraEdit.activeDocument.findReplace.matchCase=true;
             UltraEdit.activeDocument.findReplace.matchWord=false;
             UltraEdit.activeDocument.findReplace.regExp=true;
             var nNumber = 0;
             while (UltraEdit.activeDocument.findReplace.find("(?<=\\{n)\\d+(?=\\})"))
             {
                nNumber++;
                UltraEdit.activeDocument.write(nNumber.toString(10));
             }
          }
          The Perl regular expression string is now: (?<=\{n)\d+(?=\})

          This expression means that 1 or more digits should be found and selected. But valid matches are only numbers between {n and }.

          (?<=\{n) is a positive lookbehind expression which consumes zero characters (selects nothing).

          (?=\}) is a positive lookahead expression which consumes also zero characters (selects nothing).

          Lookahead and lookbehind are explained in the IDM power tip Perl Regular Expressions in UltraEdit: Digging Deeper.

          As with lookahead and lookbehind only the number is selected and not the entire string {nX}, it is more easily to replace the selected number by the incrementing number.

          The script now renumbers all numbers between {n and } in the entire file from top to bottom.

          Javascript files have usually the file extension js. For UltraEdit scripts execution it does not matter which extension the file has. But with extension js syntax highlighting is applied to the script file making it easier to read the code.

          12
          Basic UserBasic User
          12

            Mar 24, 2013#5

            My goodness! That was a jolly good answer. I did suspect that the curly brackets might be a problem. I will copy that script to a file where I have kept your very useful previous replies to my queries and will put it to the test as soon as I can.

            Thank you.

            6,603548
            Grand MasterGrand Master
            6,603548

              Mar 28, 2013#6

              I forgot to answer your second question. Here is the little script to find all occurrences of {n} and replace them by {nX} whereby X is an automatically incremented number.

              Code: Select all

              if (UltraEdit.document.length > 0)
              {
                 UltraEdit.insertMode();
                 if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
                 else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
                 UltraEdit.activeDocument.hexOff();
                 UltraEdit.ueReOn();
                 UltraEdit.activeDocument.top();
                 UltraEdit.activeDocument.findReplace.mode=0;
                 UltraEdit.activeDocument.findReplace.matchCase=true;
                 UltraEdit.activeDocument.findReplace.matchWord=false;
                 UltraEdit.activeDocument.findReplace.regExp=false;
                 UltraEdit.activeDocument.findReplace.searchDown=true;
                 if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
                 {
                    UltraEdit.activeDocument.findReplace.searchInColumn=false;
                 }
                 UltraEdit.activeDocument.findReplace.preserveCase=false;
                 UltraEdit.activeDocument.findReplace.replaceAll=false;
                 UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
              
                 var nNumber = 1;
                 while (UltraEdit.activeDocument.findReplace.replace("{n}","{n"+nNumber.toString(10)+"}")) nNumber++;
              }