Add .0D0 to the end of numbers in Vim

I have a code that I would like to translate from another language to Fortran. The code has a large numbered vector - V(n)- as well as both variables with the name tn (where n is a one-four-digit number) and numerous real numbers, which are currently written as integers. To get Fortran to treat integers as doubles, I would like to add .0D0at the end of each integer.

So, if I have an expression like:

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

I would like Vim to change it to:

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

I'm trying to use a negative look to ignore expressions starting with tor V(, and look ahead or ze to find the end of the numbers, but I'm out of luck. Anyone have any suggestions?

+4
source share
1 answer
V(1000) = t434 * 45/7 + 1296 * t18

Team:

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

Result:

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

Team:

:%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
+5
source

Source: https://habr.com/ru/post/1607942/


All Articles