Vim匹配除一行开头的字符以外的所有内容

dbz*_*VT8 4 regex vim

我已经广泛寻找答案,我无法在任何地方找到它.我想用一行(使用搜索和替换)或其他命令替换功能块中的所有代码.我还希望能够在整个文件中为多个功能执行此操作.

我有一块像这样的代码......

{
一些代码
更多代码...
许多行随机代码
}

我想用一行代码替换花括号内的所有内容,例如:

{
return STATUS_OK;
}

我尝试过类似的东西,

%s/^ {_ [^}] + /\treturn STATUS_OK;/g

但是这会停在第一行}而不是第一行}的一行开头.

我试过这个

%s/^ {_ [^^}] + /\treturn STATUS_OK;/g

为了在第一行停在第一行}但是由于某种原因这不起作用.有任何想法吗?谢谢.

per*_*eal 5

此正则表达式匹配最外层的curlies而不考虑函数名称:

 %s/^{\(\(\s\+}\)\|[^}]\|\_s\)*/{\treturn STATUS_OK;/g
Run Code Online (Sandbox Code Playgroud)

分解:

^{  # match a curly at the beginning of a line
\(  # group these alternations.... either match
     \(\s\+}\) \|  # whilespace followed by a closing curly 
                   # (to get rid of internal blocks) or ..
     [^}]      \|  # match a non curly, or ..
     \_s           # match newlines and whitespaces
 \)*               # match the alternation as long as you can
Run Code Online (Sandbox Code Playgroud)