基于vim中的正则表达式在同一行中进行多次替换

时间:2021-01-21 21:46:27

What's the regex to change "write" to "fprintf" AND (the second comma) ","..."strlen"...")" to ""?

什么是正则表达式改变“写”到“fprintf”和(第二个逗号)“,”......“strlen”......“)”到“”?

for example,

write(fd_global,"Currently inside of main after the arguments are parsed\n", strlen("Currently inside of main after the arguments are parsed\n"));

to

fprintf(fd_global, "Currently inside of main after the arguments are parsed\n");

I know "/write.*strlen" matches each line with "write"..."strlen" but that's it.

我知道“/ write.*strlen”将每一行与“write”...“strlen”匹配,但就是这样。

Thanks in advance!

提前致谢!

MY ANSWER :%s/write(/fprintf(/gc | %s/,\s*strlen((.*))/)/gc

我的答案:%s / write(/ fprintf(/ gc |%s /,\ s * strlen((。*))/)/ gc

| for two separate search and replaces in same command \s* for uncertain amount of whitespace between the second and third parameters of write

|对于写入的第二个和第三个参数之间的不确定的空白量,在同一个命令\ s *中进行两次单独的搜索和替换

2 个解决方案

#1


1  

This is what I would do:

这就是我要做的:

%s/\v^write\((.*), strlen.*/fprintf(\1);/g

#2


0  

Here's a suggestion, for an expression which will replace strlen only when it occurs inside write and calls on the same string as you've written it here:

这是一个建议,对于一个表达式,只有当它出现在write中并且调用与你在这里写的相同的字符串时才会替换strlen:

%s/write(\([^,]*\),\("[^"]*"\), strlen(\2))/fprintf(\1, \2)/gc

#1


1  

This is what I would do:

这就是我要做的:

%s/\v^write\((.*), strlen.*/fprintf(\1);/g

#2


0  

Here's a suggestion, for an expression which will replace strlen only when it occurs inside write and calls on the same string as you've written it here:

这是一个建议,对于一个表达式,只有当它出现在write中并且调用与你在这里写的相同的字符串时才会替换strlen:

%s/write(\([^,]*\),\("[^"]*"\), strlen(\2))/fprintf(\1, \2)/gc