gpt4 book ai didi

vim - 如何在 Emacs 或 Vim 中分发字符串

转载 作者:行者123 更新时间:2023-12-04 14:47:19 27 4
gpt4 key购买 nike

在 Emacs 或 Vim 中,像本例中那样连接字符串的平滑方法是什么:

Transform from:    (alpha, beta, gamma) blah (123, 456, 789)To:    (alpha=123, beta=456, gamma=789)

It would need to scale to:

  • many lines of these
  • many elements in the parentheses

I have recently found myself needing this kind of transformation often.

I use Evil in Emacs which is why a Vim answer would likely also help.

UPDATE:

The solutions were not as general as I had hoped. For example, I'd like the solution to also work when I have a list of strings and wish to distribute them into a large XML document. eg:

<item foo="" bar="barval1"/>
<item foo="" bar="barval2"/>
<item foo="" bar="barval3"/>
<item foo="" bar="barval4"/>

fooval1
fooval2
fooval3
fooval4

我制定了一个解决方案并将其添加为答案。

最佳答案

%s/(\(\S\{-}\), \(\S\{-}\), \(\S\{-}\)).\{-}(\(\S\{-}\), \(\S\{-}\), \(\S\{-}\))/(\1=\4, \2=\5, \3=\6)
%s : 全局搜索和替换
\(\S{-}\), : 非贪婪搜索直到下一个逗号的非空白字符,用“(”括起来以进行反向引用
\1=\4 : 打印出第一个匹配项,一个“=”符号,然后是第四个匹配项

关于vim - 如何在 Emacs 或 Vim 中分发字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13962720/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com