By changing the code block
Code: Select all
UltraEdit.ueReOn();
UltraEdit.document[0].findReplace.mode=0;
UltraEdit.document[0].findReplace.matchCase=true;
UltraEdit.document[0].findReplace.matchWord=true;
UltraEdit.document[0].findReplace.regExp=false;
to
Code: Select all
UltraEdit.perlReOn();
UltraEdit.document[0].findReplace.mode=0;
UltraEdit.document[0].findReplace.matchCase=true;
UltraEdit.document[0].findReplace.matchWord=false;
UltraEdit.document[0].findReplace.regExp=true;
the replaces are case-sensitive regular expression replaces using Perl regular expression engine instead of case-sensitive non regular expression replaces matching only whole words with UltraEdit regular expression engine being selected.
Now the list file can contain Perl regular expression search and replace strings separated by a single tab on each line. With this modification the search string can be
\b(?:attach|attaches|attached|attaching) .{1,30}? importance to\b
or
\battach(?:es|ed|ing)? .{1,30}? importance to\b
But be careful now with simple words in the same list file. They need now
\b at beginning and end of the word, or
\< at beginning of a single word and
\> at end of the word.
Or the script is enhanced with automatically changing the options depending on the search string.
For example the script can activate Perl regular expression options if the search string contains a non word character as the modified script below does.
Code: Select all
// The first file - most left one on the open file tabs bar - must be
// the file which should be modified.
// The file with the list of words to search for and to replace with
// must be the second file currently opened.
// The third file could be this script file which can be executed when
// it is the active file by using command "Run Active Script" from menu
// "Scripting". Or this script is added to the scripts list and executed
// from this list or by the assigned hotkey/chord.
if (UltraEdit.document.length > 1) // Are 2 files opened?
{
// Define environment for this script.
UltraEdit.insertMode();
if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
// Determine the line terminator type of the list file.
var sLineTerm = "\r\n"; // Default is DOS/Windows line termination.
if(UltraEdit.document[1].lineTerminator == 1) sLineTerm = "\n"; // Unix
else if(UltraEdit.document[1].lineTerminator == 2) sLineTerm = "\r"; // Mac
// Get entire contents of the list file into an array of
// strings whereby each string is a line from the file.
UltraEdit.document[1].selectAll();
if (UltraEdit.document[1].isSel()) // The list file should not be empty!
{
var asLines = UltraEdit.document[1].selection.split(sLineTerm);
UltraEdit.document[1].top(); // Just to cancel the selection.
// Remove the last string from the array which is most likely an empty
// string caused by the line termination on last line in the list file.
if (!asLines[asLines.length-1].length) asLines.pop();
// Move caret to top of first file, the file to modify.
UltraEdit.document[0].top();
// Define all parameters for a case-sensitive, standard (non regular
// expression) Replace All with matching and replacing only whole
// words by default to avoid that the letters of a short word being
// part of a longer word are replaced, too.
// For example, a list file containing
// today tommorow
// to from
// and "today" and "to" existing also in the file to modify would
// result in "frommmorow" and "from" without matching whole words
// only. But also possible would be "fromday" and "from" if the
// list file contains the 2 lines in the following order.
// to from
// today tommorow
// Yes, replacing words is not as easy as it looks at first sight.
// This type of replace is used for simple words in the list. A Perl
// regular expression replace is used for other search strings with
// any non word character (letter, digit, underscore).
UltraEdit.perlReOn();
UltraEdit.document[0].findReplace.mode=0;
UltraEdit.document[0].findReplace.matchCase=true;
UltraEdit.document[0].findReplace.searchDown=true;
UltraEdit.document[0].findReplace.searchInColumn=false;
UltraEdit.document[0].findReplace.preserveCase=false;
UltraEdit.document[0].findReplace.replaceAll=true;
UltraEdit.document[0].findReplace.replaceInAllOpen=false;
for (var nLine = 0; nLine < asLines.length; nLine++)
{
// Get position of horizontal tab character in line.
var nTabPosition = asLines[nLine].indexOf('\t');
// Ignore the line if it does not contain a tab character.
if (nTabPosition < 0) continue;
// The string left of tab character is the search string.
var sSearch = asLines[nLine].substring(0,nTabPosition);
// The string right of tab character is the replace string.
// Modify the replace string by inserting character ¿ after
// every character of the replace string. This avoids finding
// an already inserted string once again on subsequent replaces.
var sReplace = asLines[nLine].substring(++nTabPosition).replace(/(.)/g,"$1¿");
// Contains the search string a character which is whether
// a letter, nor a digit and also not an underscore?
if (sSearch.search(/\W/) < 0)
{
// No, run a normal replace matching only whole words.
UltraEdit.document[0].findReplace.matchWord=true;
UltraEdit.document[0].findReplace.regExp=false;
}
else
{
// Yes, run a Perl regular expression replace.
UltraEdit.document[0].findReplace.matchWord=false;
UltraEdit.document[0].findReplace.regExp=true;
}
// Run the Replace All with these 2 strings from list file.
UltraEdit.document[0].findReplace.replace(sSearch,sReplace);
}
// Remove all ¿ characters in entire file.
UltraEdit.document[0].findReplace.matchWord=false;
UltraEdit.document[0].findReplace.regExp=false;
UltraEdit.document[0].findReplace.replace("¿","");
}
}
Update on 2014-04-23: Code of script modified a little according to new requirement as written below.