2015-09-18 27 views
4

我有一段代碼需要從另一種語言轉換爲Fortran。該代碼有一個大號編號的矢量 - V(n) - 以及大量名爲tn的變量(其中n是一到四位數字)以及當前編寫爲整數的衆多實數。爲了讓Fortran將整數視爲雙精度,我想在每個整數的末尾添加.0D0將.0D0添加到Vim中的數字結尾

所以,如果我有這樣的表達:

V(1000) = t434 * 45/7 + 1296 * t18 

我想讓Vim將其改爲:

V(1000) = t434 * 45.0D0/7.0D0 + 1296.0D0 * t18 

我一直在試圖用消極的外觀背後忽略表情開始與tV(,並向前看或澤找到數字的結尾,但我沒有運氣。有沒有人有什麼建議?

回答

5
V(1000) = t434 * 45/7 + 1296 * t18 

命令:

:%s/\(\(t\|V(\)\d*\)\@<!\(\d\+\)\d\@!/\3.0D0/g 

結果:

V(1000) = t434 * 45.0D0/7.0D0 + 1296.0D0 * t18 

的命令是:

:%s/     search/replace on every line 

    \(\(t\|V(\)\d*\) t or V(, followed by no or more numbers 
         otherwise it matches 34 in t434 

    \@<!    negative lookbehind 
         to block numbers starting with t or V(

    \(\d\+\)   a run of digits - the bit we care about 

    \d\@!    negative lookahead more digits, 
         otherwise it matches 10 in 1000 

/     replace part of the search/replace 

    \3    match group 3 has the number we care about 
    .0D0    the text you want to add 

/g     global flag, apply many times in a line 
相關問題