How to only lowercase quoted string in VIM

巧了我就是萌 提交于 2019-12-01 18:05:56

问题


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'

回答1:


Try

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

'\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.


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

Take a look at :h s/\L



来源:https://stackoverflow.com/questions/18346991/how-to-only-lowercase-quoted-string-in-vim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!