Sample:
function foobar($arg1 = null, $arg2 = null) {
$_a = $arg1;
$_b = $arg2;
}
I want to write a search and replace regex in vim that does this:
我想写一个搜索并在vim中替换正则表达式:
function foobar($arg1 = null, $arg2 = null) {
$_a = (!$arg1) ? $arg1 : 1;
$_b = (!$arg2) ? $arg2 : 1;
}
This is what I've tried so far:
这是我到目前为止所尝试的:
- Using the
.
command to repeat my last action in vim, but it only appends: 1;
to the lines - I've written dozens of variations of this regex, but can't get it to work as expected:
%s/\$_a\t=\t*/(!\$arg1)\t\?\t\2\t\:\t1/g
- Replace the lines manually, but it goes without saying that I've to do this over huge number of files.
使用 。命令重复我在vim中的最后一个动作,但它只追加:1;到了线
我已经写了这个正则表达式的几十种变体,但无法按预期工作:%s / \ $ _ a \ t = \ t * / /(!\ $ arg1)\ t \?\ t \ 2 \吨\:\ T1 /克
手动替换行,但不言而喻,我必须在大量文件上执行此操作。
Basically what I want to do is to write a regex that matches $arg1
and then enclose the match with the ternary operator, but I haven't been able to access the regex matches in vim.
基本上我想要做的是编写一个匹配$ arg1的正则表达式然后将匹配与三元运算符括起来,但是我无法访问vim中的正则表达式匹配。
Any help will be greatly appreciated!
任何帮助将不胜感激!
(it doesn't have to be done in vim, I'm open to suggestions like using perl)
(它不必在vim中完成,我对使用perl这样的建议持开放态度)
Thanks!!
1 个解决方案
#1
1
Try following command:
尝试以下命令:
:%s/\(\$arg[0-9]\);/(!\1) ? \1 : 1;
Above command replace $arg1;
with (!$arg1) ? $arg1 : 1;
以上命令替换$ arg1;用(!$ arg1)? $ arg1:1;
#1
1
Try following command:
尝试以下命令:
:%s/\(\$arg[0-9]\);/(!\1) ? \1 : 1;
Above command replace $arg1;
with (!$arg1) ? $arg1 : 1;
以上命令替换$ arg1;用(!$ arg1)? $ arg1:1;