2015-12-02 79 views
0

在使用perl正則表達式的Ultraedit中,我試圖用DATA8DATA1DATA9替換字符串DATA0,依此類推。我知道如何使用DATA\d在Ultraedit的查找對話框中實現匹配。如何在Ultraedit正則表達式中進行評估替換

爲了捕獲數字,我用DATA(\d),和「替換爲」框我可以訪問以$ 1,DATA$1+8組,但顯然,這導致文本DATA0+8,這是有道理的。

是否有一個eval()可以在Ultraedit的替換對話框中完成,以修改捕獲的組變量$ 1?

我意識到這可以在與Ultraedit的JavaScript集成中完成,但我寧願能夠從Replace Dialog開箱即可完成此操作。

+0

我不明白Perl是如何在所有與此相關的工具,但也有發現的例子/替換:http://www.ultraedit.com/support/tutorials_power_tips/ultraedit/regular_expressions .html –

+0

@Hunter:它支持3種正則表達式(Ultraedit,Unix和Perl),我特別使用Perl正則表達式樣式。鏈接中的示例非常有用,但特別缺乏修改捕獲的組的能力,這是我的問題。我現在可能根本無法做到。 –

回答

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:\..." 
+0

啊,是的。太糟糕了,我仍然在使用perl 5.8。我可以使用'perl -i.bak -pe「s /(DATA)(\ d +)/ $ 1.eval($ 2 + 8)/ eg」「C:\ ...」' –

+0

擺脫那個'eval' 。你只需要$ 1($ 2 + 8)' – ikegami

+0

明白了。謝謝。 –

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)); 
    } 
} 
+0

謝謝。這對我提到的具體情況非常有用,但是我不想使用該腳本的原因是因爲它不適用於一般情況。這可以改變,以提示用戶進行正則表達式搜索並替換條款,但我期望的挑戰是不要過早評估替換條款。 –

+0

@莫菲,偉大的答案。完整的例子非常感謝你。我一直在使用UE中的正則表達式來構建一個複雜的html文件轉換過程,一旦我確定了轉換序列,我就會轉向編程語言。讓我難以置信的是從上到下編號的一組divid。我能夠抓住你的代碼,修改它,並在半小時內爲我工作。 (只花了那麼長時間,因爲我花了一段時間纔打開輸出窗口看到我的錯誤信息。哦,我想我現在可以在整個過程中使用UE JS腳本。 +多 –