regex - 在 Vim 中将 .0D0 添加到数字末尾

标签 regex vim fortran

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

相关文章:

fortran - 为什么我的程序循环两次?

debugging - Fortran查询并打印出函数或子程序名称

regex - raku:最长的比赛不做最长的比赛,但在第一场比赛后退出

python - 如何使用 Python 正则表达式获取 C++ 文件中的第一个 #include 语句?

vim - vim 中行 strip 数据的有效排序宏是什么?

swift:为每个类实例加载 dylib(或线程安全)

c# - 查找字符串中最后一位数字的正则表达式是什么?

python - 匹配模式与中间的文本

vim - 复杂的 ftdetect 插件

vim - 为什么强烈推荐 "hjkl"?