Question

I have a list of dates (YYYY-M or YYYY-MM) and want to prefix 0 before the first 9 months for consistency. Data format : Date in YYYY-M or YYYY-MM followed by a comma and a number. Eg:

2012-1,789
2012-11,563

2012-1,789 should be changed to 2012-01,789. The entry `2012-11,563' should remain unchanged.

Correct output should be:

2012-01,789
2012-11,563

I tried following regular expression in Vim.

:%s/-\(\d\),/-0\0,/g

However, I get the following output:

2012-0-1,789
2012-11,563

Why am I getting an additional dash - between two digits?

Was it helpful?

Solution

Capturing group number starts from 1, not from 0.

So the command should be:

:%s/-\(\d\),/-0\1,/g
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top