中删除包含字符串的完整单词

中删除包含字符串的完整单词

本文介绍了在 Vim 中删除包含字符串的完整单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一行中删除包含特定字符串的单词.这是文本的示例:

I am trying to remove words from a line if they contain a specific string. Here is an example of the text:

host-a, host-b, host-c+test, host-d, host-e+test

我想删除任何包含 +test 的内容,结果为:

I want to remove anything that contains +test, to result with:

host-a, host-b, host-d

同样,我需要逐行应用它,而不是文件中的所有行.它将在宏中使用.

Likewise, I need to apply this on a line by line basis, not on all lines in the file. It is going to be used within a macro.

我该怎么做?

推荐答案

可能是这样的吗?

:%s/\<[-a-z]\++test\>//gc

如果您想替换它,它会询问每个匹配项.提示:set hlsearch 将显示匹配项.

It will ask for each match if you want to replace it. Hint: set hlsearch will show you the matches.

如果您还想删除逗号,则:

If you also want to remove the comma, then:

%s/\(,\s\+\)\?\S\++test\>//gc

这篇关于在 Vim 中删除包含字符串的完整单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:20