3

I'd like to replace the following code:

ThisIsALoooongNameOfATwoDArray[100-rowIndex][j-2]-ThisIsALoooongNameOfATwoDArray[100+rowIndex][j+2];

by

Ptr[j-2]-Ptr[j+2];

I tried the command :%s/This.*ex\]/Ptr/g (and /gc variation), It turns out the regular expression performs the longest matching and the result becomes

Ptr[j+2];

Is there any simple way to achieve my goal? (I know I can delete it one by one and/or use q macro. Just wonder if we can do something using the "restricted" regular expression.)

1

You can use

s/\mThis.\{-}]/Ptr/g

\{-} will match as few as possible characters before the following character in the regex. This allows to match only the first pair of brackets.

To be more detailled:

  • \m use magic flavor of regex
  • This match literally the string This
  • .\{-}] match as few as possible character before the following ].
| improve this answer | |
1

A greedy alternative:

:%s/This[^]]*ex\]/Ptr/g

Where:

  • [^]]* matches anything but ]
    • ex\] matches literally ex]
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.