CSV搜索和替换 [英] CSV search and replace
问题描述
我想用不同的值替换一系列管道字符,我将如何使用正则表达式?
I want to replace a series of pipeline characters with different values, how would I do this with regular expressions?
示例:
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
推荐答案
如果替换值仅在两端的数字不同,请使用命令
If substitution values differ only in the numbers at the ends, use the command
:let n=[0] | %s/|/\='new'.map(n,'v:val+1')[0]/g
(请参阅我的回答问题 gVim查找/替换计数器for
详细说明技术。)
(See my answer to the question "gVim find/replace with counter" for detailed description of the technique.)
如果替换值彼此本质上不同,请更改
命令,而不是替换事例的序列号,而是替换项
In case of substitution values that differ essentially from each other, change the command to substitute not a serial number of an occurrence, but an item of a replacement list with that number as an index.
:let n=[-1] | %s/|/\=['one','two','three'][map(n,'v:val+1')[0]]/g
要在每行上独立执行替换,请使用
:global
命令通过缓冲区的
行重复上述命令之一。
To perform the substitutions on every line independently of each other, use
the :global
command to iterate one of the above commands through the lines
of a buffer.
: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
这篇关于CSV搜索和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!