如何使用VI去除匹配正则表达式的线条上的字符的晦涩?(How to use VI to remove ocurance of character on lines matching regex?)

编程入门 行业动态 更新时间:2024-10-27 10:27:23
如何使用VI去除匹配正则表达式的线条上的字符的晦涩?(How to use VI to remove ocurance of character on lines matching regex?)

我试图将一些函数的方法名称的情况从lowercase_with_underscores更改为lowerCamelCase用于以public function get_method_name()开头的行。 我很难一步完成这项工作。

到目前为止,我使用了以下内容

:%s/\(get\)\([a-zA-Z]*\)_\(\w\)/\1\2\u\3/g

但是,这一次只能替换一个_字符。 我想要一个搜索和替换,做如下的事情:

识别包含字符串public function [gs]et所有行。 在这些行上,执行以下搜索并替换:s/_\(\w\)/\u\1/g (

编辑:

假设我有行get_method_name()和set_method_name($variable_name) ,我只想更改方法名称而不是变量名称的情况,我该怎么做? get_method_name()当然更简单,但我想要一个适用于单个命令的解决方案。 我已经能够使用:%g/public function [gs]et/ . . . :%g/public function [gs]et/ . . . 根据下面列出的解决方案来解决get_method_name()情况,但遗憾的是不是set_method_name($variable_name)情况。

I'm trying to change the case of method names for some functions from lowercase_with_underscores to lowerCamelCase for lines that begin with public function get_method_name(). I'm struggling to get this done in a single step.

So far I have used the following

:%s/\(get\)\([a-zA-Z]*\)_\(\w\)/\1\2\u\3/g

However, this only replaces one _ character at a time. What I would like it a search and replace that does something like the following:

Identify all lines containing the string public function [gs]et. On these lines, perform the following search and replace :s/_\(\w\)/\u\1/g (

EDIT:

Suppose I have lines get_method_name() and set_method_name($variable_name) and I only want to change the case of the method name and not the variable name, how might I do that? The get_method_name() is more simple of course, but I'd like a solution that works for both in a single command. I've been able to use :%g/public function [gs]et/ . . . as per the solution listed below to solve for the get_method_name() case, but unfortunately not the set_method_name($variable_name) case.

最满意答案

如果我理解正确的话,我不知道为什么你尝试过的东西没有用,但是你可以使用g来对线匹配模式执行普通模式命令。

你的例子是这样的:

:%g/public function [gs]et/:s/_\(\w\)/\u\1/g

更新:

为了只匹配方法名称,我们可以使用在第一个$之前只有方法名称的事实,因为这看起来像是PHP。

要做到这一点,我们可以使用负面的lookbehind, @<! :

:%g/public function [gs]et/:s/\(\$.\+\)\@<!_\(\w\)/\u\2/g

这将看到@<! 对于任何$后跟任意数量的字符,如果没有找到$ s,则只匹配_\(\w\) 。

奖励积分(?):

要为多个缓冲区执行此操作,请在%g前面粘贴一个bufdo

If I've understood you correctly, I don't know why the things you've tried haven't worked but you can use g to perform a normal mode command on lines matchings a pattern.

Your example would be something like:

:%g/public function [gs]et/:s/_\(\w\)/\u\1/g

Update:

To match only the method names, we can use the fact that there will only be method names before the first $, as this looks to be PHP.

To do that, we can use a negative lookbehind, @<!:

:%g/public function [gs]et/:s/\(\$.\+\)\@<!_\(\w\)/\u\2/g

This will look behind @<! for any $ followed by any number of characters and only match _\(\w\) if no $s are found.

Bonus points(?):

To do this for multiple buffers stick a bufdo in front of the %g

更多推荐

本文发布于:2023-07-31 11:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1343800.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:晦涩   如何使用   线条   字符   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!