如何仅替换以特定模式开头的单词

问题描述 投票:0回答:1

我正在尝试替换具有 3 个共同字符的单词,而不是在匹配模式后替换整个句子。

示例:

He went to abcqwe a while ago.

He is in abc123 since morning.

He came from abcljkou yesterday night.

现在我想用单词

'abc'
替换以
'New'
开头的单词。这就是我期待的结果:

He went to new a while ago.

He is in abc123 since morning.

He came from new yesterday night.

我尝试使用:

%s/abc.*/new/g
,但是这个在匹配模式后会替换整行。

vim
1个回答
0
投票

我不确定您想要将哪些字符算作“单词”,但以下给出了您的情况下的预期结果:

:%s/abc[a-z]\+/new

这使用正则表达式“abc[a-z]+”(表示确切的字符串“abc”后跟一个或多个小写字母)来匹配和替换单词。如果您还想匹配大写字母,您可以添加其他范围,例如 A-Z。

© www.soinside.com 2019 - 2024. All rights reserved.