如何使用 vi(m) 中的查找和替换对部分文本重新排序?
我有这种格式的数据:
03/18/2010
03/18/2010
04/19/2010
我想将年份从每个日期字符串的末尾移动到开头,如下所示:
2010/03/18
2010/03/18
2010/04/19
我需要搜索/替换模式来执行此操作。我想我可能需要像这样使用&符号:
:%s/'[0-9]\{2\}'\/'[0-9]\{2\}'\/'[0-9]\{4\}'/&3\/&1\/&2/
或者类似的东西,但我只是不确定。这个搜索/替换可以吗?如果是这样,有人愿意启发我吗?
I have data in this format:
03/18/2010
03/18/2010
04/19/2010
I would like to move the year from the end of each date string, to the beginning, like so:
2010/03/18
2010/03/18
2010/04/19
I need search/replace pattern that will do this. I thought that I might need to use the ampersand like this:
:%s/'[0-9]\{2\}'\/'[0-9]\{2\}'\/'[0-9]\{4\}'/&3\/&1\/&2/
Or something along those lines, but I am just not sure. Is this search/replace possible? If so, would somebody be so kind as to enlighten me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然有可能。
我更改了以下内容:
我使用
+
符号,而不是使用斜杠来分隔替换命令的各个部分。分隔符不必是正斜杠。无论您在%s
之后放置什么符号,都会成为分隔符。这很有用,因为我们需要在模式中使用正斜杠。我使用(转义的)括号在正则表达式中创建组。这让我们可以通过使用反斜杠后跟组的编号来在替换模式中引用这些组。组从左到右编号,从 1 开始,组 0 是整个匹配项。
Of course it is possible.
I changed the following:
Instead of using slashes to separate between the various parts of the substitute command, I used
+
symbols. The separator does not have to be a forward slash. Whatever symbol you put after the%s
becomes the separator. This is useful because we need to use forward slashes in the patterns.I used (escaped) parenthesis to create groups within the regular expression. This lets us refer to these groups in the replace pattern by using a backslash followed by the number of the group. Groups are numbered from left to right, starting with 1 and group 0 is the entire match.