微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

正则表达式 – 将.0D0添加到Vim中的数字末尾

我有一个代码,我想从另一种语言翻译成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

我一直试图用负面看后面来忽略以t或V开头的表达式(并且向前看或者是找到数字的结尾,但是我没有运气.有没有人有任何建议?

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐