我想用不同的值替換一系列管道字符,我如何用正則表達式來做到這一點?CSV搜索和替換
實施例:
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
我想用不同的值替換一系列管道字符,我如何用正則表達式來做到這一點?CSV搜索和替換
實施例:
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
如果替換值在端部只不同的號碼,使用命令
: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
定義一個函數:
fun CountUp()
let ret = g:i
let g:i = g:i + 1
return ret
endf
現在,使用:
:let i = 1 | %s/|/\="new" . CountUp()/g
感謝您的提示。 – help
請重新表述您的問題增加大約所需更換的其他詳細信息。 –