我以前从未使用过UltraEdit,所以我在使用它时遇到了困难。我看过文档,没有看到任何有用的东西。
到目前为止,我只是尝试在用户打开的当前文件中查找单词,并删除从文件开头到单词的所有内容。
我尝试过的各种功能
UltraEdit.activeDocument这是我认为我需要使用的,但没有一个有效。我试过像这样的东西
search
goto但他们都失败了。我无法让它在用户打开的文件中搜索任何内容。
这方面的任何帮助,或者UltraEdit的javascript用户指南,都将不胜感激。
发布于 2015-04-04 18:46:09
这可以在UltraEdit中使用正则表达式替换所有从文件顶部执行的,或者使用高级replace选项Replace all从文件顶部执行。
单击Replace对话框中的Help按钮,然后按照链接转到解释UltraEdit/Unix和Perl正则表达式语法的页面。也有帮助直接在替换对话框点击按钮打开正则表达式生成器列表(按钮与放大镜或三角形取决于UE的版本)。
UltraEdit正则表达式全部替换
使用带有tagged regular expression的UltraEdit正则表达式引擎,搜索字符串为%*^(word^),替换字符串为^1。
%表示line.*的开始,表示除回车符和换行符之外的任何字符。times.^(...^) 或更多标签^1引用的替换的word,因为在运行不区分大小写的替换时,应保留word而不更改word中任何字母的大小写。搜索字符串是非贪婪的,这意味着*会在一行中第一次出现时停止匹配字符。
一个贪婪的搜索表达式,匹配一行中最后一次出现word的所有内容,应该是%?+^(word^)。?+指的是除换行符、回车符和换行符1或更多次以外的任何字符。
注意:非贪婪表达式进行了替换,word也位于行的开头,但实际上并没有替换/删除任何内容。但是,行更改指示器会将该行标记为已修改。
Unix正则表达式全部替换
对于Unix正则表达式引擎,搜索字符串需要为^.*(word),替换字符串为\1。
^表示line..*的开始,表示除回车符和换行符以外的任何字符。或多次非greedy.(...)标记\1引用的替换的,因为\1应保留word。贪婪的搜索表达式应该是^.+(test)。.+表示1或更多次。
注意:非贪婪表达式进行了替换,word也位于行的开头,但实际上并没有替换/删除任何内容。
Perl正则表达式替换全部
有了最强大的、Perl、正则表达式引擎,这项任务有很多可能性。
可以使用类似于UltraEdit/Unix regexp引擎的Perl regular expression using backreferences,使用搜索字符串^.*?(word)或^.+?(word)进行非贪婪搜索,或使用^.*(word)或^.+(word)进行贪婪搜索,并使用\1作为替换字符串。
从这里可以看出,Perl regexp引擎有一个特殊的语法,它定义了一个乘数,如* (0倍或更多倍)或+ (1倍或更多倍)贪婪或非贪婪。乘数后的?使表达式不贪婪。
使用搜索字符串^.+?(word)可以避免对以word开头的行进行任何更改。
但是Perl regexp引擎支持的更像是非匹配的lookahead。
为此任务使用先行表达式的Perl regexp搜索字符串为^.+?(?=word),替换字符串现在为空字符串。
为什么替换字符串为空?
替换字符串现在必须为空,因为搜索现在不再匹配word,只匹配从行首到第一次出现的word至少包含1个字符的字符串。
但这并不是Perl regexp引擎支持的全部内容。如果word是更长单词中的子字符串,并且替换应该删除从行首到第一次出现word是整个单词而不是更长单词的一部分的所有字符,会发生什么情况呢?
好吧,Perl regexp引擎支持\<表示单词的开始,\>表示单词的结束,\b表示任何单词的边界(开始或结束)。
例如,^.+?(?=\<word\>)或^.+?(?=\bword\b)会忽略只包含words而不包含word的行。
要求用户输入word的UltraEdit脚本
上面解释的所有可能性也可以在UltraEdit宏和脚本中使用。
脚本解决方案要求脚本用户输入一个单词,然后替换活动文件中从一行开始到第一次出现输入单词的所有内容,所需的代码如下:
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+"\\>)","");
}
}
}https://stackoverflow.com/questions/29406652
复制相似问题