我有一个代码,我想将它从另一种语言翻译成 Fortran。代码有一个大编号向量-- V(n)
-- 以及许多名为 tn 的变量(其中 n 是一到四位数字)和许多当前写为整数的实数。为了让 Fortran 将整数视为 double ,我想添加 .0D0
到每个整数的末尾。
所以如果我有这样的表达:
V(1000) = t434 * 45/7 + 1296 * t18
我希望 Vim 将其更改为:
V(1000) = t434 * 45.0D0/7.0D0 + 1296.0D0 * t18
我一直在尝试使用负向后视来忽略以
t
开头的表达式。或 V(
, 并向前看或 ze 找到数字的结尾,但我没有运气。有没有人有什么建议?
最佳答案
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
关于regex - 在 Vim 中将 .0D0 添加到数字末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32643130/