Script to extract custom regexp to a new document with CRLF

Help with writing and running scripts

Script to extract custom regexp to a new document with CRLF

Postby caveatrob » Tue Sep 02, 2008 2:42 pm

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);
caveatrob
Basic User
Basic User
 
Posts: 20
Joined: Fri Apr 25, 2008 4:39 pm

Re: Script to extract custom regexp to a new document with CRLF

Postby Mofi » Wed Sep 03, 2008 3:01 am

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

Re: Script to extract custom regexp to a new document with CRLF

Postby caveatrob » Wed Sep 03, 2008 8:14 am

Works great! Now how do I modify that script so that it pastes $1 of the match if I put in parentheses?
caveatrob
Basic User
Basic User
 
Posts: 20
Joined: Fri Apr 25, 2008 4:39 pm

Re: Script to extract custom regexp to a new document with CRLF

Postby Mofi » Wed Sep 03, 2008 8:33 am

$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.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4042
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Script to extract custom regexp to a new document with CRLF

Postby caveatrob » Wed Sep 03, 2008 8:35 am

Do you have a script example? Do you do custom scripts? I am willing to pay.
caveatrob
Basic User
Basic User
 
Posts: 20
Joined: Fri Apr 25, 2008 4:39 pm

Re: Script to extract custom regexp to a new document with CRLF

Postby Mofi » Mon Sep 08, 2008 2:44 am

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


Return to Scripts