我的文字如下.
cat
dog
elephant
cat (1)
zebra(1)
snow leopard
shark (other)
animal (hi) (2)
Run Code Online (Sandbox Code Playgroud)
我想按如下方式更换它们.
[[cat]]
[[dog]]
[[elephant]]
[[cat]] (1)
[[zebra]](1)
[[snow leopard]]
[[shark]] (other)
[[animal (hi)]] (2)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
谢谢你提前.
注意cat (1)
和zebra(1)
(行4~5)之间的区别,空格.
您可以使用非贪婪匹配尽可能少的字符.\{-}
,然后可选地匹配带括号的组,然后匹配行的末尾:
:%s/\(.\{-}\)\( \?([^)]*)\)\?$/[[\1]]\2/
Run Code Online (Sandbox Code Playgroud)