如何使用 vi(m) 中的查找和替换对部分文本重新排序?

发布于 2024-10-15 22:25:11 字数 359 浏览 7 评论 0原文

我有这种格式的数据:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤慢歌 2024-10-22 22:25:11

当然有可能。

:%s+\([0-9]\{2\}\)/\([0-9]\{2\}\)/\([0-9]\{4\}\)+\3/\1/\2+

我更改了以下内容:

  • 我使用 + 符号,而不是使用斜杠来分隔替换命令的各个部分。分隔符不必是正斜杠。无论您在 %s 之后放置什么符号,都会成为分隔符。这很有用,因为我们需要在模式中使用正斜杠。

  • 我使用(转义的)括号在正则表达式中创建组。这让我们可以通过使用反斜杠后跟组的编号来在替换模式中引用这些组。组从左到右编号,从 1 开始,组 0 是整个匹配项。

Of course it is possible.

:%s+\([0-9]\{2\}\)/\([0-9]\{2\}\)/\([0-9]\{4\}\)+\3/\1/\2+

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文