在替换中使用 vim 的 printf

Ste*_*dUp 0 regex vim

我正在尝试从 m/d/yyyy 7/1/2016 转换日期格式

每行发生一次到 YYYY-mm-dd 00:00:00

我有搜索和参数部分工作,可以交换订单等,

但是我想使用 vim 的 printf 来做到这一点,并提出了以下...

%s/\(\d\+\)\/\(\d\+\)\/\(\d\+\)/\=printf('%d-%02d-%02d 00:00:00', \3, \1, \2)/
Run Code Online (Sandbox Code Playgroud)

我得到...

E15: Invalid expression: \3, \1, \2)
E116: Invalid arguments for function printf('%d-%02d-%02d 00:00:00', \3, \1, \2)
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Men*_*ックス 6

您需要使用 submatch()功能:

:%s/\(\d\+\)\/\(\d\+\)\/\(\d\+\)/\=printf('%d-%02d-%02d 00:00:00', submatch(3),
submatch(1), submatch(2))/
Run Code Online (Sandbox Code Playgroud)
:help submatch()
Run Code Online (Sandbox Code Playgroud)
submatch({nr}[, {list}])                                submatch()
                Only for an expression in a :substitute command or
                substitute() function.
                Returns the {nr}'th submatch of the matched text.  When {nr}
                is 0 the whole matched text is returned.
                Note that a NL in the string can stand for a line break of a
                multi-line match or a NUL character in the text.
                Also see sub-replace-expression.(...)
Run Code Online (Sandbox Code Playgroud)