将特定正则表达式匹配的字符串替换为其在Vim中的长度

时间:2022-09-13 07:34:40

In Vim, how do I replace a string matched by certain regexp with its length, like this:

在Vim中,如何将特定regexp匹配的字符串替换为其长度,如下所示:

Regexp = "\w*"

Regexp =“\ w *”

"hello" -> "5"
"bye" -> "3"

“你好” - >“5”“bye” - >“3”

Is it possible to do it with :s or do I need to write some script?

是否可以使用:s或我是否需要编写一些脚本?

Best regards

最好的祝福

1 个解决方案

#1


12  

Replace 'hello' with your regex expression and this should work in VIM:

用你的正则表达式替换'hello',这应该在VIM中起作用:

:s/hello/\=strlen(submatch(0))/ 

#1


12  

Replace 'hello' with your regex expression and this should work in VIM:

用你的正则表达式替换'hello',这应该在VIM中起作用:

:s/hello/\=strlen(submatch(0))/