将.0D0添加到Vim

时间:2015-09-18 03:00:33

标签: regex vim fortran

我有一个代码,我想从另一种语言翻译成Fortran。代码有一个大数字向量 - V(n) - 以及名为tn的众多变量(其中n是一到四位数字)和许多当前写为整数的实数。为了让Fortran将整数视为双精度,我想将.0D0添加到每个整数的末尾。

所以,如果我有一个像:

这样的表达式
V(1000) = t434 * 45/7 + 1296 * t18

我希望Vim将其更改为:

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

我一直在尝试使用负面观察来忽略以tV(开头的表达式,然后向前看或者找到数字的结尾,但是我'我没有运气。有没有人有任何建议?

1 个答案:

答案 0 :(得分: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