Tapatalk

How to get tagged parts of a found string into string variables?

How to get tagged parts of a found string into string variables?

74
Advanced UserAdvanced User
74

    Jan 16, 2012#1

    I'm trying to make a simple find and replace function but am having problems with the output report. It's important the user gets an output report telling them what was found and what it was replaced with.

    Code: Select all

    var sFound= '(<\/end><first><second{1} id=".*?"><note>)([\\s\\S?]+?<para>)';
    var sReplacedNote= '$1<para level="4" change="add" rfc="statement">' + sReplaced+ '</para>$2';
    
    
    if (sFirstStep){
    	UltraEdit.activeDocument.top();
    	UltraEdit.activeDocument.findReplace.replace(sFound,sReplacedNote);
    }
    If I try to get the output window to show what's in sReplacedNote it literally gives me back the string with out the replacement groupings (So I still get $1 and $2 instead of what's in those groups).

    Can anyone point out what I'm doing wrong?
    Thanks,
    Max

    6,685587
    Grand MasterGrand Master
    6,685587

      Jan 17, 2012#2

      You don't have access to the found string parts referenced by $1 and $2. This replacement happens deeply inside the Perl regular expression function. If you want parts of the found string written to output window, you need to evaluate the found string by yourself in the script and then make the replace. This can be done by following code in your case:

      Code: Select all

      var sFound= '(</end><first><second id=".*?"><note>)([\\s\\S?]+?<para>)';
      // var sReplacedNote= '$1<para level="4" change="add" rfc="statement">' + sReplaced+ '</para>$2';
      
      
      if (sFirstStep){
         UltraEdit.activeDocument.top();
         if (UltraEdit.activeDocument.findReplace.find(sFound))
         {
            var sReplacedNote = UltraEdit.activeDocument.selection.replace(/<note>/,"<note><para level=\"4\" change=\"add\" rfc=\"statement\">" + sReplaced + "</para>");
            UltraEdit.activeDocument.write(sReplacedNote);
         }
      }
      So the Replace command is replaced by a Find command. If a string is found, it is selected. This selected string is taken and converted now within the script into the replace string you want. sReplacedNote contains then the string which can be written to output window and which is written into the file overwritting the found string.

      A few more notes:
      The character / (forward slash) has no special meaning in regular expressions. Therefore it is not necessary to escape / with a backslash as you have done at beginning of the search string.

      {1} in a Perl regular expression means the previous character or expression exactly 1 times. I don't see any reason for {1} after character d of the word second. If you omit {1} you specify that next character after second must be a space character and therefore secondd would be automatically ignored like when using {1}.

      74
      Advanced UserAdvanced User
      74

        Jan 17, 2012#3

        I think I understand what you mean, but still need guidance on how to get the groups in the replace notes.
        var sFound= '(</end><first><second id=".*?"><note>)([\\s\\S?]+?<para>)';
        // var sReplacedNote= '$1<para level="4" change="add" rfc="statement">' + sReplaced+ '</para>$2';
        So would I add group 1 & 2 in here?

        Code: Select all

        var sReplacedNote = UltraEdit.activeDocument.selection.replace(/<note>/,"$1<para level=\"4\" change=\"add\" rfc=\"statement\">" + sReplaced + "$2");
        Thank you for your help,
        Max

        6,685587
        Grand MasterGrand Master
        6,685587

          Jan 18, 2012#4

          I don't understand your new question because I have given already the solution. In your case the first part of the found string ends always with fixed string <note> and everything after this string belongs to the second part. The Find command selects the entire found string. The command

          var sReplacedNote = UltraEdit.activeDocument.selection.replace(/<note>/,"<note><para level=\"4\" change=\"add\" rfc=\"statement\">" + sReplaced + "</para>");

          uses the replace method of the Javascript string object to create a copy of the selected string in variable sReplacedNote where first occurrence of the fixed string <note> is replaced by "<note><para level="4" change="add" rfc="statement">", the value of variable sReplaced and fixed string "</para>". So variable sReplacedNote contains the replace string for the found string which is written next into the file overwritting the selected found string like the Perl regular expression Replace command would have done and which you can write also to the output window.

          If you want first and second part of found string in separate string variables, you can use following code in this case:

          Code: Select all

          // Get index of character after first occurence of string "<note>" in the selected string.
          var nLengthFirstPart = UltraEdit.activeDocument.selection.indexOf("<note>") + 6;  // "<note>" has 6 characters.
          
          // Copy nLengthFirstPart characters starting from index 0 into a string variable named sFirstPart.
          var sFirstPart = UltraEdit.activeDocument.selection.substr(0,nLengthFirstPart);
          
          // The length of first part of found string is equal the start index of the second part because
          // strings are character with first element having index number 0. Copy all characters starting
          // from index equal first part length to end of string into a string variable named sSecondPart.
          var sSecondPart = UltraEdit.activeDocument.selection.substr(nLengthFirstPart);

          21
          Basic UserBasic User
          21

            Jan 18, 2012#5

            Code: Select all

            //Create testcase
            UltraEdit.newFile();
            UltraEdit.activeDocument.write('<\/end><first><second id="11"><note>22<para>\n');
            
            var sFound= '(<\/end><first><second{1} id=".*?"><note>)([\\s\\S?]+?<para>)';
            var sReplaced = 'test';
            var sReplacedNote= '$1<para level="4" change="add" rfc="statement">' + sReplaced+ '</para>$2';
            
            UltraEdit.outputWindow.show = true;
            
            UltraEdit.activeDocument.findReplace.regExp = true;
            
            var sFirstStep = true;
            
            if (sFirstStep){
               UltraEdit.activeDocument.top();
               //Need replace?
               if ( UltraEdit.activeDocument.findReplace.find( sFound ) ) {
               		//store original value (it's marked)
                  var orgVal = UltraEdit.activeDocument.selection;
            			UltraEdit.outputWindow.write( "OrgValue: " + orgVal );
            			UltraEdit.activeDocument.top();
            			//do replace just with first term
            			UltraEdit.activeDocument.findReplace.replace(sFound,"$1");
            			//get value for replacevalue1
                  var repVal1 = UltraEdit.activeDocument.selection;
            			UltraEdit.outputWindow.write( "RepValue1: " + repVal1 );
            			//restore original value
            			UltraEdit.activeDocument.write( orgVal );
            			UltraEdit.activeDocument.top();
            			//do replace just with second term
            			UltraEdit.activeDocument.findReplace.replace(sFound,"$2");
            			//get value for replacevalue2
                  var repVal2 = UltraEdit.activeDocument.selection;
            			UltraEdit.outputWindow.write( "RepValue2: " + repVal2 );
            			//restore original value
            			UltraEdit.activeDocument.write( orgVal );
            			//finally do the replace as wanted
            			UltraEdit.activeDocument.top();
            			UltraEdit.activeDocument.findReplace.replace(sFound,sReplacedNote);
                  var repVal = UltraEdit.activeDocument.selection;
            			UltraEdit.outputWindow.write( "RepValue: " + repVal );
               }
            }