如何在vi / vim编辑器中替换多行中的特定字符串

问题描述 投票:-1回答:2

我在文件中包含以下几行:

|other words in line| abcd_1234.xyz |other words in line|

|other words in line| abcd_2345.xyz |other words in line|

|other words in line| abcd_3456.xyz |other words in line|

|other words in line| abcd_4567.xyz |other words in line|

我想将以上几行更改为

|other words in line| q |other words in line|

|other words in line| q |other words in line|

|other words in line| q |other words in line|

|other words in line| q |other words in line|

我尝试过:%s/abcd*.xyz/q/g,但这似乎不起作用。请任何人建议如何解决此问题。

regex vim replace vi
2个回答
2
投票

尝试以下操作:

:%s/(abcd_[0-9]{4}\.xyz)/q/g 

0
投票

如果我们可以指望有空格和|,那么您可以使用:

:%s/\|(.*)\| (abcd_.*) \|(.*)\|/\1q\3

PS:您可以使用https://regex101.com/r/DHLbIx/1帮助您进行匹配

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