s.ouchene
s.ouchene

Reputation: 1869

Vim: Adding * symbol between numbers and the left parenthesis

I have thousands of lines in a file in the following format:

x1(t) = 1.568(1-t) + 5.145(1-t)**2 + ... (other terms)
x2(t) = 3.347(1-t) + 1.304(1-t)**2 + ...
x3(t) = 7.016(1-t) + 1.901(1-t)**2 + ...
x4(t) = 0.843(1-t) + 5.335(1-t)**2 + ...
....


As you can see, there is no * sign between the numbers and the left parenthesis. I could record a macro to fix that ,but for some reason I do like to use the :substitute command with regular expressions, instead.

I've tried the following:

:%s/[0-9]([0-9]/*(/g

But that does substitute also the digits before and after the left parenthesis. I don't know how to match the parenthesis alone without matching the numbers before and after.

I appreciate your help.

Upvotes: 2

Views: 87

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

You may use

:%s/[0-9]\zs(\ze[0-9]/*(/g

This is roughly equivalent to a [0-9]\K\((?=[0-9]) PCRE regex and matches:

  • [0-9] - a digit
  • \zs - omit the text matched so far from match memory buffer
  • ( - a ( char
  • \ze - end of consuming pattern, the rest is context
  • [0-9] - a digit must appear after ( (the digit is just context, not part of a match).

Upvotes: 1

Related Questions