多行正则表达式模式删除空行

时间:2013-04-11 10:09:44

标签: regex vim multiline blank-line

/* Comments for code... */

if (...) {

}

我需要删除评论与if

之间的空白行
/* Comments for code... */
if (...) {

}

我目前正在使用以下正则表达式:

/\*\/\ze\n^$\n[ ]*if
  • \*//:评论结束(*/
  • ^$if
  • 之前的空白行
  • [ ]*if:空格和if

当我使用\ze时,光标最终指向*/。我该怎么办?

3 个答案:

答案 0 :(得分:2)

试试这一行:

%s#\*/[\s\r\n]*#*/\r#

它将成为

/* Comments for code... */





if (...) {

}
/* Comments for code... */






else{


}

成:

/* Comments for code... */
if (...) {

}
/* Comments for code... */
else{


}

答案 1 :(得分:1)

为什么不同时使用\zs

这对我有用:

:%s/\*\/\zs\n*[ ]*\zeif/\r/g

阐释:

%s - substitution on the entire file
\*\/ - end of comment
\zs - start of match
\n*[ ]* - eol and spaces
\ze - end of match
if - followed by if
/\n/ - replacement
g - global regex (multiline)

答案 2 :(得分:1)

:g+*/+j

更快但可能太宽。

您可以执行以下操作:

:g+*/\_\s*if+j
相关问题