如何评估Ultraedit正则表达式替换

时间:2015-12-02 14:45:30

标签: regex perl ultraedit

在使用perl Regex的Ultraedit中,我尝试将DATA0替换为DATA8DATA1替换为DATA9,依此类推。我知道如何使用DATA\d在Ultraedit的查找对话框中进行匹配。

为了捕获数字,我使用DATA(\d),并在"替换为:"框我可以使用$ 1 DATA$1+8访问该论坛,但这显然会产生文字DATA0+8,这是有道理的。

是否可以在Ultraedit的替换对话框中执行eval()以修改捕获的组变量$ 1?

我意识到这可以在与Ultraedit的javascript集成中完成,但我宁愿能够从Replace Dialog中开箱即用。

2 个答案:

答案 0 :(得分:2)

不,UltraEdit不能这样做。

你实际上可以使用Perl

perl -i.bak -pe"s/DATA\K(\d+)/$1+8/eg" "C:\..."       5.10+

perl -i.bak -pe"s/(DATA)(\d+)/$1.($2+8)/eg" "C:\..."

答案 1 :(得分:1)

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));
   }
}
相关问题