How to reference a search result and put into a variable

Help with writing and running scripts

How to reference a search result and put into a variable

Postby bluesky » Thu Mar 28, 2013 11:22 am

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
bluesky
Newbie
 
Posts: 1
Joined: Thu Mar 28, 2013 11:15 am

Re: How to reference a search result and put into a variable

Postby Mofi » Fri Mar 29, 2013 5:21 am

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>");
   }
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Scripts