I was going to comment on another answer but it got complicated.
Mind the magic
setting. If you want unescaped parens to do grouping, you need to include \v
somewhere in your pattern. (See :help magic
).
You can avoid escaping the slashes if you use something other than slashes in the :s
command.
You are close. :) You don't want all of those spaces though as they'll require spaces in the same places to match.
My solution, where I use \v
so I don't need to escape the parens and exclamation points so I can use slashes in my pattern without escaping them:
:%s!\v(\d{2})/(\d{2})/(\d{2})!20\3-\2-\1!g
This will match "inside" items that start or end with three or more digits though, too. If you can give begin/end criteria then that'd possibly be helpful. Assuming that simple "word boundary" conditions work, you can use <>
:
:%s!\v<(\d{2})/(\d{2})/(\d{2})>!20\3-\2-\1!g
To critique yours specifically (for learning!):
:%s/ ( \d{2} ) ( \/\d{2} ) ( \/\d{2} ) / ( 20+\3) - (\3) - (\1) /g
- Get rid of the spaces since presumably you don't want them!
- Your grouping needs either
\( \)
or \v
to work
- You also need
\{2}
unless you use \v
- You are putting the slashes in groups two and three which means they'll show up in the replacement too
- You don't want the parentheses in the output!
- You're substituting text directly; you don't want the
+
after the 20 in the output