如何在 VIM 中只小写带引号的字符串 [英] How to only lowercase quoted string in VIM

查看:59
本文介绍了如何在 VIM 中只小写带引号的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含以下内容的文件:

Say I have a file with the following content:

Apple 'BANANA' ORANGE 'PEACH'

将所有引用的大写字母转换为小写字母的正则表达式是什么?

What is the regex to convert all quoted uppercase to lowercase?

预期的输出文件应如下所示:

The expected output file should look like:

Apple 'banana' ORANGE 'peach'

推荐答案

尝试

:%s/'\w\+'/\=tolower(submatch(0))/g

'\w\+' 匹配引号内的任何单词.并将其替换为匹配的小写版本.\= 告诉替换计算表达式 tolower(submatch(0)) 其中 tolower() 切换 submatch(0)(整个匹配)到小写.

'\w\+' match any word that is inside quotes. and replace it with the lowercase version of the match. \= tells substitute to evaluate the expression tolower(submatch(0)) where tolower() switches the string found in submatch(0) (the whole match) to lower case.

您也可以使用 \L 原子将后面的字符串转为小写,\0submatch(0) 相同代码>

You can also use the \L atom to turn the string after it to lower case and \0 is the same as submatch(0)

:%s/'\w\+'/\L\0/g

看看:h s/\L

这篇关于如何在 VIM 中只小写带引号的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆