Find text depending on an entered word and a number

Help with writing and running scripts

Find text depending on an entered word and a number

Postby bitmap007 » Sat Apr 14, 2012 4:06 am

Hi all, sorry about my English. I need a script do this work: I have a text file and if we enter a word and a number we'll get a new file including the word and following words. The number of words should depend on the number entered during script execution. Thanks so much.
bitmap007
Basic User
Basic User
 
Posts: 14
Joined: Tue Jun 14, 2011 12:15 am

Re: Find text depending on an entered word and a number

Postby Mofi » Sat Apr 14, 2012 5:02 am

Here is the script which uses a Perl regular expression search in a loop to find all occurrences of the word and the additional words according to the entered number and output them in a new file.

Code: Select all
if (UltraEdit.document.length > 0) {

   var sFound = "";  // Initialize the variable for found strings.
   // Define working environment. Insert/overwrite mode is not changed.
   UltraEdit.columnModeOff();
   UltraEdit.perlReOn();

   // Ask user of script for word to find. Do not allow an empty string.
   do {
      var sWord = UltraEdit.getString("Word to find:",1);
   }
   while (!sWord.length);

   // Ask user of script for number of additional words to find. Do not
   // allow a negative number although in UE v18.00.0.1034 a negative
   // number can't be entered by the user.
   do {
      var nNumber = UltraEdit.getValue("Number of additional words:",1);
   } while (nNumber < 0);

   // Define most of the find parameters. The search is executed from top
   // of active file to end of file in a loop to find all occurrences.
   UltraEdit.activeDocument.findReplace.mode=0;
   UltraEdit.activeDocument.findReplace.matchCase=false;
   UltraEdit.activeDocument.findReplace.searchDown=true;
   if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean") {
      UltraEdit.activeDocument.findReplace.searchInColumn=false;
   }

   UltraEdit.activeDocument.top();  // Move caret to top of active file.
   
   if (!nNumber) {
      // If the number of additional words is 0, run a simple not
      // case-sensitive search for just the entered word in a loop.
      UltraEdit.activeDocument.findReplace.matchWord=true;
      UltraEdit.activeDocument.findReplace.regExp=false;
      // If the word is found, copy it to string variable.
      while (UltraEdit.activeDocument.findReplace.find(sWord)) {
         sFound += UltraEdit.activeDocument.selection + "\r\n";
         // It depends on your version of UltraEdit if the following lines
         // are necessary or not. They are not needed in UE v18.00.0.1034.
         // UltraEdit.activeDocument.endSelect();
         // UltraEdit.activeDocument.key("LEFT ARROW");
         // UltraEdit.activeDocument.key("RIGHT ARROW");
      }
   } else {
      // If additional words should be also found, run a Perl regular
      // expression find searching for the entered word and the additional
      // words according to entered number. The regular expression to use
      // is "\<WordToFind(?:\W+\w+){X}" with X being the entered number.
      //      \<     means begin search on start of a word.
      // (?: ... )   is a non marking group.
      //      \W+    non word character 1 or more times.
      //      \w+    word character (including underscore) 1 or more times.
      //      {X}    the expression in the preceding non marking group
      //             matching a sequence of non word and a sequence of
      //             word characters exactly X times.
      UltraEdit.activeDocument.findReplace.matchWord=false;
      UltraEdit.activeDocument.findReplace.regExp=true;
      var sSearch = "\\<" + sWord + "(?:\\W+\\w+){" + nNumber.toString() + "}";
      while (UltraEdit.activeDocument.findReplace.find(sSearch)) {
         sFound += UltraEdit.activeDocument.selection + "\r\n";
         // It depends on your version of UltraEdit if the following lines
         // are necessary or not. They are not needed in UE v18.00.0.1034.
         // UltraEdit.activeDocument.endSelect();
         // UltraEdit.activeDocument.key("LEFT ARROW");
         // UltraEdit.activeDocument.key("RIGHT ARROW");
      }
   }

   // Open a new file and write to it the found string or the error
   // message. Finally move the caret in new file to top of the file.
   if (!sFound.length) sFound = "Nothing found."
   UltraEdit.newFile();
   UltraEdit.activeDocument.write(sFound);
   UltraEdit.activeDocument.top();
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3937
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Find text depending on an entered word and a number

Postby bitmap007 » Sat Apr 14, 2012 8:07 pm

Hi mofi, thank for your help.
bitmap007
Basic User
Basic User
 
Posts: 14
Joined: Tue Jun 14, 2011 12:15 am


Return to Scripts