开发者

How can I reorder parts of text with a find and replace in vi(m)?

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜