2

Am i correct to understand, that the definition

:range s[ubstitute]/pattern/string/cgiI

suggests that in the string part indeed only strings are to be used, that is patterns not allowed? What i would like to do is do replacement of say any N symbols at position M with X*N symbols, so i would have liked to use something like this:

:%s/^\(.\{10}\).\{28}/\1X\{28}/g

Which does not work because \{28} is interpreted literally.

Is writing the 28 XXXXX...X in the replace part the only possibility?

4

3 回答 3

2

替换部分中唯一允许的正则表达式构造是编号组:\1 \2 \3 等。重复构造{28}在那里无效,尽管这是一个聪明的主意。您必须使用 28 个 X。

于 2012-06-19T13:05:37.273 回答
2

您可以通过在替换部分中使用表达式\=。您必须通过 访问匹配项submatch(),并将其与静态字符串连接在一起,您可以通过以下方式生成该字符串repeat()

:%s/^\(.\{10}\).\{28}/\=submatch(1) . repeat('X',28)/g
于 2012-06-19T14:08:22.610 回答
1

另一种选择是在替换部分中使用表达式:

:%s/^\(.\{10}\).\{28}/\=submatch(1).repeat("X",28)/g

第一个匹配组是用 获得的submatch(1)。有关详细信息,请参阅:h sub-replace-expression

于 2012-06-19T14:07:49.783 回答