我想用不同的值替换一系列管道字符,我将如何使用正则表达式?
示例:
This | is | a | sentence
And | this | is | the | second | one
最终结果:
This new is new2 a new3 sentence
And new this new2 is new3 the new4 second new5 one
答案 0 :(得分:2)
如果替换值仅在末尾的数字上有所不同,请使用命令
:let n=[0] | %s/|/\='new'.map(n,'v:val+1')[0]/g
(请参阅my answer问题“gVim find/replace with counter” 该技术的详细描述。)
如果替换值彼此不同,则更改 命令不是替换事件的序列号,而是替换项的序列号 以该号码作为索引的替换列表。
:let n=[-1] | %s/|/\=['one','two','three'][map(n,'v:val+1')[0]]/g
要在彼此独立的每条线上执行替换,请使用
:global
命令通过行迭代上述命令之一
一个缓冲区。
:g/^/let n=[0] | s/|/\='new'.map(n,'v:val+1')[0]/g
类似地,
:g/^/let n=[-1] | s/|/\=['one','two','three'][map(n,'v:val+1')[0]]/g
答案 1 :(得分:0)
定义一个函数:
fun CountUp()
let ret = g:i
let g:i = g:i + 1
return ret
endf
现在,使用:
:let i = 1 | %s/|/\="new" . CountUp()/g