New to regex and I need to pattern match on some dates to change the format.
I’m going from mm/dd/yy to yyyy-mm-dd where there are no entries prior to 2000.
What I’m unfamiliar with is how to group things to use their respective references of \1, \2, etc.
Would I first want to match on mm/dd/yy with something like ( \d{2} ) ( \/\d{2} ) ( \/\d{2} ) or is it as easy as \d\d/\d\d/\d\d ?
Assuming my first grouping is partially the right idea, I’m looking to do something like:
:%s/old/new/g
:%s/ ( \d{2} ) ( \/\d{2} ) ( \/\d{2} ) / ( 20+\3) - (\3) - (\1) /g
EDIT: Sorry, the replace is going to a yyyy-mm-dd format with hyphens, not the slash.
I was going to comment on another answer but it got complicated.
Mind the
magicsetting. If you want unescaped parens to do grouping, you need to include\vsomewhere in your pattern. (See:help magic).You can avoid escaping the slashes if you use something other than slashes in the
:scommand.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
\vso I don’t need to escape the parens and exclamation points so I can use slashes in my pattern without escaping them: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
<>:To critique yours specifically (for learning!):
\( \)or\vto work\{2}unless you use\v+after the 20 in the output