[英]vim search and replace expression by using previously matched pattern
I have multiple lines in my data file like below 我的数据文件中有多行,如下所示
<some-text>:<fixed-string>
and I want to add space around the colon to feed to some other tool that expects this. 并且我想在冒号周围添加空间以填充到其他期望达到此目的的工具。 I would like to do this in vim (not sed or other tools).
我想在vim中这样做(而不是sed或其他工具)。
I was trying to do something like 我正在尝试做类似的事情
%s/[a-z]*:<fixed-string>/\1 : <fixed-string>/gc
this doesn't seem to work. 这似乎不起作用。 Can someone please help ?
有人可以帮忙吗? To take care of upper & lower case I tried using (\\a)* thats next step.
为了照顾大写和小写,我尝试了下一步(\\ a)*。
You do not have any capture groups so you can not use \\1
. 您没有任何捕获组,因此无法使用
\\1
。 You either need to use capture groups, \\zs
& \\ze
, or look-behinds and look-aheads. 您或者需要使用捕获组
\\zs
和\\ze
,或者使用后视和前瞻。
With a capture group: 使用捕获组:
:%s/\(\l*\):fixed/\1 : fixed/gc
With \\zs
and \\ze
to set the start and end of the match 使用
\\zs
和\\ze
设置比赛的开始和结束
:%s/\l*\zs:\zefixed/ : /gc
Look-aheads, \\@=
, and Look-behinds, \\@=
: 前瞻
\\@=
和后瞻\\@=
:
:%s/\(\l*\)\@<=:\(fixed\)\@=/ : /gc
Personally, I use use \\zs
and \\ze
as they are the simplest to reason about and use. 我个人使用use
\\zs
和\\ze
因为它们是最简单的推理和使用方法。 I am also using \\l
which is short hand for [az]
. 我也使用
\\l
,它是[az]
简写。
For more help see: 有关更多帮助,请参见:
:h /\(
:h /\zs
:h /\@<=
:h /\@=
:h /\l
please try this: 请尝试以下操作:
%s/\\(.*\\):fixed-string$/\\1 : fixed-string/g %s / \\(。* \\):固定字符串$ / \\ 1:固定字符串/ g
or this: 或这个:
%s/:fixed-string$/ : fixed-string/g %s /:fixed-string $ /:固定字符串/ g
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.