我的计划是做一个非常标准的搜索替换,用old_string
替换new_string
的所有实例。问题是我只想在特定old_strings
之后为任意数量的prefix
执行此操作。例如:
old_string = "a"
new_string = "b"
prefix = "xxxx"
xxxxaaaaaaaa => xxxxbbbbbbbb
xxxxaaapostfix => xxxxbbbpostfix
xxaaaa => xxaaaa
等。我不知道该怎么做。我想有一些方法可以说s/xxxxa*/xxxxb{number of a's}/g
或其他什么,但我不知道它是什么。
答案 0 :(得分:5)
你绝对可以做到这一点!我会使用\=
寄存器来评估一些vimscript。来自:h s/\=
:
Substitute with an expression *sub-replace-expression*
*sub-replace-\=* *s/\=*
When the substitute string starts with "\=" the remainder is interpreted as an
expression.
The special meaning for characters as mentioned at |sub-replace-special| does
not apply except for "<CR>". A <NL> character is used as a line break, you
can get one with a double-quote string: "\n". Prepend a backslash to get a
real <NL> character (which will be a NUL in the file).
然后,您可以使用repeat
和submatch
函数构建正确的字符串。例如:
:%s/\(xxxx\)\(a\+\)/\=submatch(1).repeat('b', len(submatch(2)))
我选择使用\+
而不是*
,因为在替换命令完成后将找不到该模式(此效果hlsearch
和 n )
当然,如果你使用\zs
和\ze
(匹配的开始/结束)原子,你可以使用更少的捕获组,这使得这个更短更清晰。
:%s/xxxx\zsa\+/\=repeat('b', len(submatch(0)))
答案 1 :(得分:1)
如果您有perl
支持,则可以使用
:%perldo s/xxxx\Ka+/"b" x length($&)/ge
xxxx\Ka+
仅在前面有a
xxxx
匹配
/ge
替换所有匹配项,e
允许在替换部分中使用Perl代码"b" x length($&)
字符串b
重复length($&)
次有关详细信息,请参阅:h perl