如何在 Ultraedit 中评估正则表达式替换
How to eval in Ultraedit regex Replace
在使用 perl Regex 的 Ultraedit 中,我尝试将字符串 DATA0
替换为 DATA8
,将 DATA1
替换为 DATA9
,等等。我知道如何使用 DATA\d
在 Ultraedit 的查找对话框中进行匹配。
为了捕获数字,我使用 DATA(\d)
,在 "Replace With:" 框中我可以使用 $1 访问该组,DATA+8
但这显然会导致文本DATA0+8
,有道理。
是否可以在 Ultraedit 的替换对话框中完成 eval()
以修改捕获的组变量 $1?
我知道这可以在 javascript 与 Ultraedit 的集成中完成,但我更希望能够从替换对话框中开箱即用。
不,UltraEdit 做不到。
您实际上可以使用 Perl
perl -i.bak -pe"s/DATA\K(\d+)/+8/eg" "C:\..." 5.10+
perl -i.bak -pe"s/(DATA)(\d+)/.(+8)/eg" "C:\..."
像 UltraEdit 这样的文本编辑器不支持在替换操作期间计算公式。这需要脚本和脚本解释器,如 Perl 或 JavaScript.
UltraEdit 内置了 JavaScript 解释器。因此,这个任务也可以通过 UltraEdit 使用 UltraEdit 脚本来完成,例如下面的脚本。
if (UltraEdit.document.length > 0) // Is any file opened?
{
// Define environment for this script.
UltraEdit.insertMode();
UltraEdit.columnModeOff();
// Move caret to top of the active file.
UltraEdit.activeDocument.top();
// Defined all Perl regular expression Find parameters.
UltraEdit.perlReOn();
UltraEdit.activeDocument.findReplace.mode=0;
UltraEdit.activeDocument.findReplace.matchCase=true;
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;
}
// Search for each number after case-sensitive word DATA using
// a look-behind to get just the number selected by the find.
// Each backslash in search string for Perl regular expression
// engine of UltraEdit must be escaped with one more backslash as
// the backslash is also the escape character in JavaScript strings.
while(UltraEdit.activeDocument.findReplace.find("(?<=\bDATA)\d+"))
{
// Convert found and selected string to an integer using decimal
// system, increment the number by eight, convert the incremented
// number back to a string using again decimal system and write the
// increased number string to file overwriting the selected number.
var nNumber = parseInt(UltraEdit.activeDocument.selection,10) + 8;
UltraEdit.activeDocument.write(nNumber.toString(10));
}
}
在使用 perl Regex 的 Ultraedit 中,我尝试将字符串 DATA0
替换为 DATA8
,将 DATA1
替换为 DATA9
,等等。我知道如何使用 DATA\d
在 Ultraedit 的查找对话框中进行匹配。
为了捕获数字,我使用 DATA(\d)
,在 "Replace With:" 框中我可以使用 $1 访问该组,DATA+8
但这显然会导致文本DATA0+8
,有道理。
是否可以在 Ultraedit 的替换对话框中完成 eval()
以修改捕获的组变量 $1?
我知道这可以在 javascript 与 Ultraedit 的集成中完成,但我更希望能够从替换对话框中开箱即用。
不,UltraEdit 做不到。
您实际上可以使用 Perl
perl -i.bak -pe"s/DATA\K(\d+)/+8/eg" "C:\..." 5.10+
perl -i.bak -pe"s/(DATA)(\d+)/.(+8)/eg" "C:\..."
像 UltraEdit 这样的文本编辑器不支持在替换操作期间计算公式。这需要脚本和脚本解释器,如 Perl 或 JavaScript.
UltraEdit 内置了 JavaScript 解释器。因此,这个任务也可以通过 UltraEdit 使用 UltraEdit 脚本来完成,例如下面的脚本。
if (UltraEdit.document.length > 0) // Is any file opened?
{
// Define environment for this script.
UltraEdit.insertMode();
UltraEdit.columnModeOff();
// Move caret to top of the active file.
UltraEdit.activeDocument.top();
// Defined all Perl regular expression Find parameters.
UltraEdit.perlReOn();
UltraEdit.activeDocument.findReplace.mode=0;
UltraEdit.activeDocument.findReplace.matchCase=true;
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;
}
// Search for each number after case-sensitive word DATA using
// a look-behind to get just the number selected by the find.
// Each backslash in search string for Perl regular expression
// engine of UltraEdit must be escaped with one more backslash as
// the backslash is also the escape character in JavaScript strings.
while(UltraEdit.activeDocument.findReplace.find("(?<=\bDATA)\d+"))
{
// Convert found and selected string to an integer using decimal
// system, increment the number by eight, convert the incremented
// number back to a string using again decimal system and write the
// increased number string to file overwriting the selected number.
var nNumber = parseInt(UltraEdit.activeDocument.selection,10) + 8;
UltraEdit.activeDocument.write(nNumber.toString(10));
}
}