How to reference a search result and put into a variable

How to reference a search result and put into a variable

1
NewbieNewbie
1

    Mar 28, 2013#1

    This is what I want the script to do
    <com style="h1"><font style="b"><num>8:6</num>RULES OF CONSTRUCTION</font></com>
    <com style="h2"><font style="b"><num>(a)</num>A Paten</com>
    <com style="h3"><font style="i"><num>(i)</num>A Paten</com>

    become
    <com style="h1"><font style="b"><num>8:6</num>RULES OF CONSTRUCTION</font></com>
    <com style="h2"><font style="b">8:6<num>(a)</num>A Paten</com>
    <com style="h3"><font style="i">8:6 (a)<num>(i)</num>A Paten</com>

    I already got my search part works, but i don't know how to store the search results into a variable.

    UltraEdit.perlReOn(); /* Let's use UE's own regexp syntax */
    UltraEdit.activeDocument.findReplace.regExp = true;
    UltraEdit.activeDocument.findReplace.replaceAll = false;
    UltraEdit.activeDocument.top();
    var regexType = UltraEdit.regexMode;

    UltraEdit.activeDocument.findReplace.find("<com style=\"h1(.*?)<num>(.*?)<\\/num>");

    var h1 = \\1; <-- doesn't work
    var h2 = \\2;

    Can someone help me on this issue?
    The reason I need to store the group reference is because I need to run another search and insert the value I found from first search into second search.
    I am using Version 17.30.0.1002
    Thank you

    6,686585
    Grand MasterGrand Master
    6,686585

      Mar 29, 2013#2

      See the script code below.

      Code: Select all

      UltraEdit.perlReOn(); /* Let's use UE's own regexp syntax */
      // Define once all parameters for the Perl regular expression find/replace
      // to make script independent on internal defaults of UltraEdit.
      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;
      UltraEdit.activeDocument.findReplace.preserveCase=false;
      UltraEdit.activeDocument.findReplace.replaceAll=false;
      UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
      UltraEdit.activeDocument.top();
      if (UltraEdit.activeDocument.findReplace.find("<com style=\"h1.*?<num>.*?</num>"))
      {
         // It is not possible to access parts of the found string marked (tagged) outside
         // the find method. They are not stored in global buffers. They are stored during
         // the find in dynamically allocated memory buffers within the Find function and
         // therefore do not exist anymore now after Find function has terminated.
         // However the string found by above Perl regular expression is selected now in
         // active file and therefore it is possible to apply the replace method of the
         // Javascript String object to get the strings of interest into string variables.
         // Please note that ^ matches in the regular expression now beginning of selected
         // string and $ matches end of selected string. The RegExp object of Javascript
         // core is used here and not the more powerful Perl regexp engine of UltraEdit.
         var sNumH1 = UltraEdit.activeDocument.selection.replace(/^.*?<num>(.*?)<\/num>.*$/,"$1");
         // Search for heading 2.
         if (UltraEdit.activeDocument.findReplace.find("<com style=\"h2.*?<num>.*?</num>"))
         {
            // Heading 2 found. Get the number of heading 2.
            var sNumH2 = UltraEdit.activeDocument.selection.replace(/^.*?<num>(.*?)<\/num>.*$/,"$1");
            // overwrite the selected heading 2 by same string with heading number 1 inserted.
            var sHeading2 = UltraEdit.activeDocument.selection.replace(/<num>/,sNumH1+"<num>");
            UltraEdit.activeDocument.write(sHeading2);
            // Use a Perl regexp replace to insert heading number 1 and 2 into heading 3.
            UltraEdit.activeDocument.findReplace.replace("(<com style=\"h3.*?)<num>","\\1"+sNumH1+" "+sNumH2+"<num>");
         }
      }

      20
      Basic UserBasic User
      20

        Find/replace within string variable

        Feb 09, 2014#3

        There's a distinction, I've learnt from Mofi, between the replace function of the UltraEdit document object and the replace function of a Javascript String object. (JSO)
        I capture and define a new string variable TITLE like so, and it works just fine:

        Code: Select all

        while(UltraEdit.activeDocument.findReplace.find("<\\$!!TITLE_(.*?)>"))
        {
           var TITLE = UltraEdit.activeDocument.selection;
        }
        The variable TITLE is now a JSO, right? In my understanding of this, it means that I can now apply to it either the Javascript replace code:

        Code: Select all

        var TITLE = str.replace("<\\$!!TITLE_","");
        or a Perl regex command:

        Code: Select all

        var TITLE =~ s/<\$!!TITLE_(.*?)>/$1/;
        Neither of these lines is working. What am I doing wrong?
        best, and thank you.
        fvgfvg

        6,686585
        Grand MasterGrand Master
        6,686585

          Re: Find/replace within string variable

          Feb 09, 2014#4

          Let's say there is a file containing the following line:

          Code: Select all

          Some text before <$!!TITLE_Example for a title> and some text after the title.
          And we run on this file a script with following code:

          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();
          
             UltraEdit.perlReOn();
             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;
          
             UltraEdit.outputWindow.showStatus=false;
             UltraEdit.outputWindow.clear();
             UltraEdit.outputWindow.showWindow(true);
             
             UltraEdit.outputWindow.write("First version of getting the title:\n===================================");
             UltraEdit.activeDocument.top();
             if(UltraEdit.activeDocument.findReplace.find("<\\$!!TITLE_.*?>"))
             {
                var sFoundTitle = UltraEdit.activeDocument.selection;
                sTitle1 = sFoundTitle.replace(/<\$!!TITLE_(.*?)>/,"$1");
                UltraEdit.outputWindow.write("sFoundTitle = "+sFoundTitle);
                UltraEdit.outputWindow.write("sTitle1 = "+sTitle1);
             }
          
             UltraEdit.outputWindow.write("\nSecond version of getting the title:\n====================================");
             UltraEdit.activeDocument.top();
             if(UltraEdit.activeDocument.findReplace.find("<\\$!!TITLE_.*?>"))
             {
                var sTitle2 = UltraEdit.activeDocument.selection;
                UltraEdit.outputWindow.write("sTitle2 (before replace) = "+sTitle2);
                sTitle2 = sTitle2.replace(/<\$!!TITLE_(.*?)>/,"$1");
                UltraEdit.outputWindow.write("sTitle2 (after replace)  = "+sTitle2);
             }
          
             UltraEdit.outputWindow.write("\nThird version of getting the title:\n===================================");
             UltraEdit.activeDocument.top();
             if(UltraEdit.activeDocument.findReplace.find("<\\$!!TITLE_.*?>"))
             {
                var sTitle3 = UltraEdit.activeDocument.selection.replace(/<\$!!TITLE_(.*?)>/,"$1");
                UltraEdit.outputWindow.write("sTitle3 = "+sTitle3);
             }
          
             UltraEdit.outputWindow.write("\nFourth version of getting the title:\n====================================");
             UltraEdit.activeDocument.top();
             if(UltraEdit.activeDocument.findReplace.find("(?<=<\\$!!TITLE_)[^>]+"))
             {
                var sTitle4 = UltraEdit.activeDocument.selection;
                UltraEdit.outputWindow.write("sTitle4 = "+sTitle4);
             }
          }
          
          The output window would contain now the information:

          Code: Select all

          First version of getting the title:
          ===================================
          sFoundTitle = <$!!TITLE_Example for a title>
          sTitle1 = Example for a title
          
          Second version of getting the title:
          ====================================
          sTitle2 (before replace) = <$!!TITLE_Example for a title>
          sTitle2 (after replace)  = Example for a title
          
          Third version of getting the title:
          ===================================
          sTitle3 = Example for a title
          
          Fourth version of getting the title:
          ====================================
          sTitle4 = Example for a title
          You see here the 4 possibilities to get the string you want into a variable.


          About first version:

          The first version finds the entire title string plus the surrounding tags you do not want. The round brackets are useless here as you do not back reference the part of the found string matched by .*? directly in the search string nor is the command a replace command. It is not possible to get a part of the found string hold internally in the Perl regular find/replace function for the find/replace only outside the function.

          The found string is selected by UltraEdit in the active document and UltraEdit creates a copy of the selected text into a JavaScript string object. This text is next copied once more in memory to one more dynamically created string object with name sFoundTitle. Next a replace is done on the value of sFoundTitle. But the replace method of a JavaScript String object never modifies the string value of the string object on which the replace method is applied. The replace command always results in creating a new string in memory which is next assigned as a value to a new string object with name sTitle1.


          About second version:

          This is nearly the same as first version. The difference is that after creating in memory the new string with the replaces applied, the original and up to this point still unmodified string object sTitle2 is destroyed (removed from memory) and a new string object is created with name sTitle2 (same as the other string object before) which is associated with the newly created string value in memory.


          About third version:

          As UltraEdit.activeDocument.selection is a JavaScript string object containing a copy of the selected text in active document, it is possible to apply the replace method directly on this string object. As the replace method does not replace the value of string object UltraEdit.activeDocument.selection, but creates a new string in memory with the modifications according to regular expression search string and the replace string applied, the new string object sTitle3 created next contains then as value the string of interest.


          About fourth version:

          The Perl regular expression search string is modified to use a positive lookbehind resulting in getting found and therefore selected only the part of the entire title tag which should be really found and processed further.
          Best regards from an UC/UE/UES for Windows user from Austria

          20
          Basic UserBasic User
          20

            Re: Find/replace within string variable

            Feb 10, 2014#5

            Thank you very much. I've now learnt that it's possible to select a string, parse it and assign it to a variable all in one command. Elegant.

            It's the last alternative:

            Code: Select all

                if(UltraEdit.activeDocument.findReplace.find("(?<=<\\$!!TITLE_)[^>]+"))
                   {
                      var sTitle4 = UltraEdit.activeDocument.selection;
                   }
            And I've now learnt how to do a regex on a string:

            Code: Select all

             sTitle2 = sTitle2.replace(/<\$!!TITLE_(.*?)>/,"$1");
            Really neat. Thanks for the patient explanation.
            best,
            fvgfvg

              problems still: regex on string

              Feb 20, 2014#6

              I have text like this:

              Code: Select all

              [1] text text text text text.
              [2] text text text text text.
              .....
              [40] text text text text.
              The following routine should capture the digit (or string?) into footnoteNr.
              In RegexBuddy it works, according to the Mozilla documentation it should work, but in fact it doesn't.
              What am I doing wrong?

              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();       // Regex Perl
                 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;
                 UltraEdit.activeDocument.findReplace.preserveCase=false;
                 UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
                 UltraEdit.activeDocument.findReplace.replaceAll=false;
                 if (UltraEdit.outputWindow.visible == false) UltraEdit.outputWindow.showWindow(true);
                 UltraEdit.activeDocument.top();
                 while(UltraEdit.activeDocument.findReplace.find("^\\[(\\d*?)\\](.*?)$"))
                 {
                    var findStr = UltraEdit.activeDocument.selection;
                    var footnoteNr = findStr.replace("^\\[(\\d*?)\\]","$1"); //<--
                    UltraEdit.outputWindow.write("footnoteNr:" + footnoteNr);
                 }
              }    // <---END
              // EOF
              What I need are two variables: footnoteNr and footnoteText. I've spent a couple of days now, reading through the Mozilla Javascript regex documentation. Help would be most appreciated...
              best, fvgfvg

              6,686585
              Grand MasterGrand Master
              6,686585

                Feb 21, 2014#7

                If you read carefully documentation of String.prototype.replace() you should know what you have made wrong in your script. The first parameter of the replace method of the JavaScript String object can be either a RegExp object for a regular expression replace, or a simple string which is replaced by a new string.

                But you cannot specify a string which contains a regular expression. If the first parameter is of type string, it is interpreted as simple string to replace. Regular expression replaces can be made only with a RegExp object as first parameter, not with a string containing a regular expression.

                Next, if you want to get just a part of the string by using a RegExp object, it is necessary to define the regular expression to match the entire string. Everything from original string value not matched by the expression is copied to the new string created by the replace method.

                This is the reason why I have used in the script code below also ^ for matching beginning of the string and .*$ for matching everything to end of the string. Now the regular expression of the RegExp object matches with a single regular expression search the entire string value, but marked with the parentheses for copying to new string and referenced by $1 is only the number between the square brackets.

                Last, the capturing groups, the parentheses, in the Perl regular expression search string are useless here as not back referenced within the search string itself.

                Code: Select all

                  while(UltraEdit.activeDocument.findReplace.find("^\\[\\d+?\\].*$"))
                   {
                      var findStr = UltraEdit.activeDocument.selection;
                      var footnoteNr = findStr.replace(/^\[(\d+?)\].*$/,"$1");
                      UltraEdit.outputWindow.write("footnoteNr:" + footnoteNr);
                   }
                Best regards from an UC/UE/UES for Windows user from Austria

                20
                Basic UserBasic User
                20

                  Feb 21, 2014#8

                  Right. Will try to get my mind around that. Thank you.
                  best, fvgfvg