How to use a regex backreference as a number, not string?

How to use a regex backreference as a number, not string?

40
Basic UserBasic User
40

    Jul 02, 2015#1

    Hi friends,

    I have a XML file with lots of tags like these
    <dataLink readWrite="R/W" attribute="value" tag="CurrentRecipe.CurrentSelectedSet.82.Value" class="RecipeMgrWgt" widgetType="Project" sourceType="Recipe" dataSource="_RecipeMgr"/>
    <dataLink readWrite="R/W" attribute="value" tag="CurrentRecipe.CurrentSelectedSet.157.Value" class="RecipeMgrWgt" widgetType="Project" sourceType="Recipe" dataSource="_RecipeMgr">
    My goal is to find the numbers highlighted in red in the above code and increment them with a given (same for all) value, let's say 10. So the desired result is
    <dataLink readWrite="R/W" attribute="value" tag="CurrentRecipe.CurrentSelectedSet.92.Value" class=...>
    <dataLink readWrite="R/W" attribute="value" tag="CurrentRecipe.CurrentSelectedSet.167.Value" class=...>
    For this I use a javascript with a regex that is supposed to capture the number in a backreference, then I want to increment it and put it back in the place of the captured number. However this doesn't seem to work - the number is correctly captured, but always treated as a string. The result I get is either original number replaced with NaN, or replaced with a concatenated string - 10+82 looks like 1082, not as 92.
    My entire script is below, how to modify it to do what I need?

    Thanks for any suggestions.

    Code: Select all

    if (UltraEdit.document.length > 0)     // Is any file currently opened?
        {
        	
           UltraEdit.insertMode();             // Define environment for the script.
           UltraEdit.columnModeOff();
           UltraEdit.activeDocument.top();
           UltraEdit.perlReOn();               // Define the parameters for the replace.
           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;
           if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
           {
              UltraEdit.activeDocument.findReplace.searchInColumn=false;
           }
           UltraEdit.activeDocument.findReplace.preserveCase=false;
           UltraEdit.activeDocument.findReplace.replaceAll=true;
           UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
           // Every backslash in a JavaScript string must be escaped with a backslash to
           // pass the find string correct to the UE/UES Perl regular expression engine.
     
            var s1 = 'tag="CurrentRecipe.CurrentSelectedSet.';
        		var s2 = '.Value';
        		var offst = 10; 
           UltraEdit.activeDocument.findReplace.replace(
           	'tag="CurrentRecipe\\.CurrentSelectedSet\\.(\\d{1,3}?)\\.Value', 
           	s1 + (offst + Number('\\1')) + s2
           	);
        }
    

    6,681583
    Grand MasterGrand Master
    6,681583

      Jul 03, 2015#2

      That is not really difficult, but can't be done with a Replace All.

      Code: Select all

      if (UltraEdit.document.length > 0)     // Is any file currently opened?
      {
         UltraEdit.insertMode();             // Define environment for the script.
         UltraEdit.columnModeOff();
         UltraEdit.activeDocument.top();
         UltraEdit.perlReOn();               // Define the parameters for the finds.
         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;
         if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
         {
            UltraEdit.activeDocument.findReplace.searchInColumn=false;
         }
      
         // Every backslash in a JavaScript string must be escaped with a backslash to
         // pass the find string correct to the UE/UES Perl regular expression engine.
         while (UltraEdit.activeDocument.findReplace.find("(?<=CurrentRecipe\\.CurrentSelectedSet\\.)\\d+(?=\\.Value)"))
         {
            var nNumber = parseInt(UltraEdit.activeDocument.selection,10) + 10;
            UltraEdit.activeDocument.write(nNumber.toString(10));
         }
         UltraEdit.activeDocument.top();
      }
      
      A positive look-behind and a positive look-ahead is used to match ONLY the number which should be modified in right context.

      The JavaScript core function parseInt is used to convert the found string to an integer using base 10 (decimal system).

      The JavaScript core method toString of Number object (variable) is used to convert the modified integer back to string using decimal system which is written over the selected number in file.
      Best regards from an UC/UE/UES for Windows user from Austria

      40
      Basic UserBasic User
      40

        Jul 03, 2015#3

        Thank you very much Mofi !
        Works fine.
        I also tried with parseInt in a different way but it didn't work.