简体   繁体   English

如何在VIM中使用sed将== string更改为eq(string)?

[英]How can I change == string to eq(string) with sed in VIM?

I can replace == with eq( but I am unsure how to grab the string after == and put it back in the replacement with a ) at the end. 我可以用eq替换==(但我不确定如何在==之后抓取字符串并在最后将其放回替换中)。 Does anyone have a solution? 有没有人有办法解决吗?

I was using :%s/== /eq(/g but I need to get the ) at the end of the existing string. 我在现有字符串的末尾使用:%s/== /eq(/g但我需要得到)。

I tried :%s/== .*/eq(&)/g but when I ran it I realized that is also brought the "== " into my replacement string that I wanted to get rid of. 我尝试过:%s/== .*/eq(&)/g但是当我运行它时,我意识到这也将“==”带入我想要摆脱的替换字符串中。

I guess I was hoping there was a way to capture an unknown string and put that exact string back into the replacement line. 我想我希望有一种方法来捕获一个未知的字符串并将该确切的字符串放回到替换行中。 something like :%s/== .*/eq(.*)/g where the .* is the unknown string. 类似于:%s/== .*/eq(.*)/g ,其中。*是未知字符串。 The first part works and grabs the string, but not sure how to put it back in on the replacement line. 第一部分工作并抓住字符串,但不知道如何将其放回替换线上。 Especially because I do not know how many characters/words the string may be. 特别是因为我不知道字符串可能有多少个字符/单词。

SOLUTION: :%s/== \\(.*\\)/eq(\\1)/ 解决方案:: :%s/== \\(.*\\)/eq(\\1)/

You need to capture the text 你需要捕获文本

:%s/== \(.*\)/eq(\1)/

You don't need the g flag, since the "string" extends to the end of the line, hence there will only ever be one match. 你不需要g标志,因为“string”延伸到行的末尾,因此只会有一个匹配。

使用反向引用应该有效:

:%s/== \(.*\)/eq(\1)/g

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM