I have a text as follows.
cat
dog
elephant
cat (1)
zebra(1)
snow leopard
shark (other)
animal (hi) (2)
and I want to replace them as follows.
[[cat]]
[[dog]]
[[elephant]]
[[cat]] (1)
[[zebra]](1)
[[snow leopard]]
[[shark]] (other)
[[animal (hi)]] (2)
Any ideas?
Thank you for advance.
Note the difference between cat (1)
and zebra(1)
(line 4~5), the space.
You could match as few characters as possible with a non-greedy .\{-}
, then optionally match a parenthesized group, then match the end of the line:
:%s/\(.\{-}\)\( \?([^)]*)\)\?$/[[\1]]\2/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With