Regex 更换vim中除括号部分以外的所有字符串

Regex 更换vim中除括号部分以外的所有字符串,regex,vim,Regex,Vim,我有一个文本如下 cat dog elephant cat (1) zebra(1) snow leopard shark (other) animal (hi) (2) [[cat]] [[dog]] [[elephant]] [[cat]] (1) [[zebra]](1) [[snow leopard]] [[shark]] (other) [[animal (hi)]] (2) 我想替换它们如下 cat dog elephant cat (1) zebra(1) snow leopa

我有一个文本如下

cat
dog
elephant
cat (1)
zebra(1)
snow leopard
shark (other)
animal (hi) (2)
[[cat]]
[[dog]]
[[elephant]]
[[cat]] (1)
[[zebra]](1)
[[snow leopard]]
[[shark]] (other)
[[animal (hi)]] (2)
我想替换它们如下

cat
dog
elephant
cat (1)
zebra(1)
snow leopard
shark (other)
animal (hi) (2)
[[cat]]
[[dog]]
[[elephant]]
[[cat]] (1)
[[zebra]](1)
[[snow leopard]]
[[shark]] (other)
[[animal (hi)]] (2)
有什么想法吗

谢谢你提前通知我


请注意
cat(1)
zebra(1)
(第4~5行)之间的差异,空格。

您可以使用非贪婪的
\{-}
匹配尽可能少的字符,然后可以选择匹配带括号的组,然后匹配行的末尾:

:%s/\(.\{-}\)\( \?([^)]*)\)\?$/[[\1]]\2/

我使用非常神奇的正则表达式的解决方案:

/\v(^\w+(\s\w+)?)
:%s,,[[\1]],g

First the search
\v ......... stats very magic (avoiding lots of scapes)
( .......... starts group one
^ .......... beginning of line
\w+ ........ at least one word
( .......... starts group two inside group one (it will become optional at the end
\s ........  space
\w+ ........ another word 
) ........... closes groupo two
? ........... makes group two optional inside groupo one
) ........... closes group one

你能解释一下
\@=
的意思吗?@plhn:这是一个前瞻性断言。前面的
\($\\\(\)
组已测试但不匹配。非常感谢您的建议,但我不得不修改我的问题。它有一个错误。@plhn:Updated.Ryan,如果我想处理像
animal(hi)(2)
这样的新类型,您有什么想法吗?