Find text depending on an entered word and a number

Find text depending on an entered word and a number

12
Basic UserBasic User
12

    Apr 14, 2012#1

    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.

    6,603548
    Grand MasterGrand Master
    6,603548

      Apr 14, 2012#2

      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();
      }

      12
      Basic UserBasic User
      12

        Apr 15, 2012#3

        Hi mofi, thank for your help.