Script to extract custom regexp to a new document with CRLF

Script to extract custom regexp to a new document with CRLF

23
Basic UserBasic User
23

    Sep 02, 2008#1

    Hi all,

    I need a script to extract the items matching a regexp entered from the keyboard into a new file with a carriage return between. I'd optionally like to pull out $1 from the match.

    Any ideas? This is what I have so far:

    Code: Select all

        // Is any file currently open?
           UltraEdit.perlReOn();
           UltraEdit.activeDocument.hexOff();
           UltraEdit.activeDocument.bottom();
           // Is the last line of the file terminated with a line ending?
           if (UltraEdit.activeDocument.isColNumGt(1)) {
              // No, insert missing line ending (DOS, UNIX or MAC).
              UltraEdit.insertMode();
              UltraEdit.columnModeOff();
              UltraEdit.activeDocument.insertLine();
              // If auto indent is enabled and last line starts with white-spaces,
              // delete the automatically inserted white-spaces in the new last line.
              if (UltraEdit.activeDocument.isColNumGt(1)) {
                 UltraEdit.activeDocument.deleteToStartOfLine();
              }
           }
           UltraEdit.activeDocument.top();
           var ActiveClipboard = UltraEdit.clipboardIdx;
           UltraEdit.selectClipboard(9);
           UltraEdit.clearClipboard();
           UltraEdit.activeDocument.findReplace.mode=0;
           UltraEdit.activeDocument.findReplace.searchDown=true;
           UltraEdit.activeDocument.findReplace.matchCase=false;
           UltraEdit.activeDocument.findReplace.matchWord=false;
           UltraEdit.activeDocument.findReplace.regExp=true;
           var bFound = false;
           var myExpression= UltraEdit.getString("Please enter the expression:",1);
           while (UltraEdit.activeDocument.findReplace.find(myExpression)) {
              UltraEdit.activeDocument.copyAppend();
              bFound = true;
           }
           UltraEdit.activeDocument.top();
           if (bFound) {
              UltraEdit.newFile();
              UltraEdit.activeDocument.paste();
              UltraEdit.activeDocument.top();
              UltraEdit.clearClipboard();
           } else UltraEdit.messageBox("There is no line starting with a period!");
           UltraEdit.selectClipboard(ActiveClipboard);
    

    6,686585
    Grand MasterGrand Master
    6,686585

      Sep 03, 2008#2

      You have to select and copy + append a line termination to the clipboard after every found string. Here is your script modified to do this which worked with UE v14.10.0.1024.

      Code: Select all

          // Is any file currently open?
             UltraEdit.perlReOn();
             UltraEdit.activeDocument.hexOff();
             UltraEdit.activeDocument.bottom();
             // Is the last line of the file terminated with a line ending?
             if (UltraEdit.activeDocument.isColNumGt(1)) {
                // No, insert missing line ending (DOS, UNIX or MAC).
                UltraEdit.insertMode();
                UltraEdit.columnModeOff();
                UltraEdit.activeDocument.insertLine();
                // If auto indent is enabled and last line starts with white-spaces,
                // delete the automatically inserted white-spaces in the new last line.
                if (UltraEdit.activeDocument.isColNumGt(1)) {
                   UltraEdit.activeDocument.deleteToStartOfLine();
                }
             }
             UltraEdit.activeDocument.top();
             var ActiveClipboard = UltraEdit.clipboardIdx;
             UltraEdit.selectClipboard(9);
             UltraEdit.clearClipboard();
             UltraEdit.activeDocument.findReplace.mode=0;
             UltraEdit.activeDocument.findReplace.searchDown=true;
             UltraEdit.activeDocument.findReplace.matchCase=false;
             UltraEdit.activeDocument.findReplace.matchWord=false;
             UltraEdit.activeDocument.findReplace.regExp=true;
             var bFound = false;
             var ActLineNum = 1;
             var ActColNum = 1;
             var myExpression= UltraEdit.getString("Please enter the expression:",1);
             while (UltraEdit.activeDocument.findReplace.find(myExpression)) {
                UltraEdit.activeDocument.copyAppend();
                bFound = true;
                // Remember actual cursor position at end of the found string!
                ActLineNum = UltraEdit.activeDocument.currentLineNum;
                ActColNum  = UltraEdit.activeDocument.currentColumnNum;
                if (typeof(UltraEdit.activeDocumentIdx) == "undefined") ActColNum++;
                // Find next DOS, MAC or UNIX line ending and append it to the current
                // clipboard. A Perl regular expression search is already enabled!
                if (UltraEdit.activeDocument.findReplace.find("(\\r\\n|\\r|\\n)") ) {
                   UltraEdit.activeDocument.copyAppend();
                } else {
                   // Looks like the cursor is already at the end of the file - search upwards.
                   UltraEdit.activeDocument.findReplace.searchDown=false;
                   if (UltraEdit.activeDocument.findReplace.find("(\\r\\n|\\r|\\n)") ) {
                      UltraEdit.activeDocument.copyAppend();
                   } /* else {
                      No special code is needed for files without any line termination because
                      this file surely always contains at least one at end of file because of
                      the last line termination check at top of this script.
                   }*/
                   UltraEdit.activeDocument.findReplace.searchDown=true;
                }
                // Go back to end of the found string before continueing.
                UltraEdit.activeDocument.gotoLine(ActLineNum,ActColNum);
             }
             UltraEdit.activeDocument.top();
             if (bFound) {
                UltraEdit.newFile();
                UltraEdit.activeDocument.paste();
                UltraEdit.activeDocument.top();
                UltraEdit.clearClipboard();
             } else UltraEdit.messageBox("There is no line starting with a period!");
             UltraEdit.selectClipboard(ActiveClipboard);
      Best regards from an UC/UE/UES for Windows user from Austria

      23
      Basic UserBasic User
      23

        Sep 03, 2008#3

        Works great! Now how do I modify that script so that it pastes $1 of the match if I put in parentheses?

        6,686585
        Grand MasterGrand Master
        6,686585

          Sep 03, 2008#4

          $1 is for back reference in the search or replace string, not for the selection. UltraEdit always selects the entire string found. If you must search for a larger text, but want only a part of it, run a second search inside the current selection to select only the part of interest and copy this second found string to the clipboard. Search in "selected text only" requires UE v14.00 or later.
          Best regards from an UC/UE/UES for Windows user from Austria

          23
          Basic UserBasic User
          23

            Sep 03, 2008#5

            Do you have a script example? Do you do custom scripts? I am willing to pay.

            6,686585
            Grand MasterGrand Master
            6,686585

              Sep 08, 2008#6

              I wanted to code the few lines which runs the find in the selected text. But then I had a better idea. In the new file with all the found strings line by line, move the cursor to top and run a replace all with the same search string used for collecting the strings and with "$1" as replace string. That would be much faster and easier. Also the script would be downward compatible. Here are the 2 additional lines necessary to do this. There could be regular expression search strings were this fast method does not work.

              Code: Select all

                     if (bFound) {
                        UltraEdit.newFile();
                        UltraEdit.activeDocument.paste();
                        UltraEdit.activeDocument.top();
                        UltraEdit.activeDocument.findReplace.replaceAll=true;
                        UltraEdit.activeDocument.findReplace.replace(myExpression, "$1");
                        UltraEdit.clearClipboard();
                     }
              Best regards from an UC/UE/UES for Windows user from Austria