0

I've never used UltraEdit before, so I'm having difficulty with it. I've looked at the documentation and didn't see anything helpful.

As of right now, I'm just trying to look for a word in the current file that the user has open, and remove everything from the start of the file to the word.

I've tried various functions of

UltraEdit.activeDocument

which is what I believe I need to be using, but none have worked. I've tried things like

search
goto

but they all fail. I can't get it to search for anything in the file that the user has open.

Any help with this or maybe a guide user guide for UltraEdit with javascript would be appreciated.

user2951249
  • 87
  • 1
  • 2
  • 7

1 Answers1

1

This can be most easily done in UltraEdit with a regular expression Replace All executed from top of the file or using advanced replace option Replace all is from top of file.

Click on Help button in Replace dialog and follow the links to the pages explaining UltraEdit/Unix and Perl regular expression syntax. There is also help directly in Replace dialog by clicking on button for opening regular expression builder list (button with magnifying glass or triangle depending on version of UE).

UltraEdit regular expression Replace All

Using the UltraEdit regular expression engine with a tagged regular expression the search string is %*^(word^) and the replace string is just ^1.

  • % means beginning of a line.
  • * means any character except carriage return and line-feed 0 or more times.
  • ^(...^) tags the word for the replace referenced by ^1 as the word should be kept without changing the case of any letter in word on running a case-insensitive replace.

The search string is non greedy which means * stops matching characters on first occurrence of word in a line.

A greedy search expression matching everything up to last occurrence of word in a line would be %?+^(word^). ?+ means any character except the newline characters carriage return and line-feed 1 or more times.

Note: The non greedy expression makes a replace also with word being at beginning of line although nothing is really replaced/removed. But line change indicator would mark that line nevertheless as being modified.

Unix regular expression Replace All

With the Unix regular expression engine the search string needs to be ^.*(word) and the replace string is \1.

  • ^ means beginning of a line.
  • .* means any character except carriage return and line-feed 0 or more times non greedy.
  • (...) tags the word for the replace referenced by \1 as the word should be kept.

The greedy search expression would be ^.+(test). .+ means 1 or more times.

Note: The non greedy expression makes a replace also with word being at beginning of line although nothing is really replaced/removed.

Perl regular expression Replace All

With the most powerful Perl regular expression engine there are many possibilities for this task.

It is possible to use a Perl regular expression using backreferences like with UltraEdit/Unix regexp engines using search string ^.*?(word) or ^.+?(word) for a non greedy or ^.*(word) or ^.+(word) for a greedy search and using \1 as replace string.

As it can be seen here, the Perl regexp engine has a special syntax do define a multiplier like * (0 or more times) or + (1 or more times) greedy or non greedy. A ? after the multiplier makes the expression non greedy.

Using the search string ^.+?(word) would avoid making a replace changing nothing on a line starting with word.

But Perl regexp engine supports even more like a non matching lookahead.

The Perl regexp search string using a lookahead expression for this task is ^.+?(?=word) and the replace string is now an empty string.

Why an empty replace string?

The replace string must be empty now because word is now not matched anymore by the search, just the string from beginning of line to first occurrence of word having at least 1 character.

But that's not all the Perl regexp engine supports. What about situations with word being a substring in longer words and the replace should delete all characters from beginning of a line to first occurrence of word being an entire word and not part of a longer word?

Well, Perl regexp engine supports \< meaning beginning of a word and \> meaning end of a word and \b meaning any word boundary (beginning or end).

^.+?(?=\<word\>) or ^.+?(?=\bword\b) would for example ignore a line containing just words but not word.

UltraEdit script asking user for word

All the possibilities explained above can be also used in UltraEdit macros and scripts.

The necessary code for a scripting solution asking the user of the script for a word and then replacing anything from start of a line to first occurrence of entered word in active file would be:

if (UltraEdit.document.length > 0)  // Is any file opened?
{
   // Ask user of the script for the word to search for.
   var sWord = UltraEdit.getString("Please enter the word:",1);

   // Has the script user entered anything at all?
   if (sWord.length)
   {
      // Has the user really entered a word, i.e. the string does not contain
      // any non word character (not being a letter, digit or underscore)?
      if (sWord.search(/\W/) < 0)
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         UltraEdit.columnModeOff();

         // Move caret to top of the active file.
         UltraEdit.activeDocument.top();

         // Define all the Perl regular expression Replace All options.
         UltraEdit.perlReOn();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=false;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean") {
            UltraEdit.activeDocument.findReplace.searchInColumn=false;
         }
         UltraEdit.activeDocument.findReplace.preserveCase=false;
         UltraEdit.activeDocument.findReplace.replaceAll=true;
         UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;

         // In JavaScript strings the backslash character is the escape
         // character. Therefore it is necessary to escape each backslash
         // with one more backslash to pass to the Perl regular expression
         // engine the search string containing the two backslashes.

         // Execute the Replace All from top of file.
         UltraEdit.activeDocument.findReplace.replace("^.+?(?=\\<"+sWord+"\\>)","");
      }
   }
}
Mofi
  • 46,139
  • 17
  • 80
  • 143