gpt4 book ai didi

regex - 在替换中使用 vim 的 printf

转载 作者:行者123 更新时间:2023-12-05 09:19:17 24 4
gpt4 key购买 nike

我正在尝试将日期格式从月/日/年 2016 年 7 月 1 日

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

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

但是我想使用 vim 的 printf 来完成它并想出了以下...

%s/\(\d\+\)\/\(\d\+\)\/\(\d\+\)/\=printf('%d-%02d-%02d 00:00:00', \3, \1, \2)/

我明白了...

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

有什么想法吗?

最佳答案

你需要使用submatch()函数:

:%s/\(\d\+\)\/\(\d\+\)\/\(\d\+\)/\=printf('%d-%02d-%02d 00:00:00', submatch(3),
submatch(1), submatch(2))/

:help submatch()
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.(...)

关于regex - 在替换中使用 vim 的 printf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41330466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com