正则表达式在所有文件的函数调用中查找/替换参数模式 [英] Regex to Find/replace argument pattern in a function-call across all files

查看:26
本文介绍了正则表达式在所有文件的函数调用中查找/替换参数模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的代码库,我们需要在其中对特定函数的参数进行模式更改.

I have a large codebase, where we need to make a pattern-change in the argument of a specific function.

即函数 foo() 的所有参数都从格式 something.anotherThing 重命名为 something_anotherThing

i.e. All arguments to a function foo() are renamed from the format something.anotherThing are to be renamed as something_anotherThing

参数可以是任何东西,但总是采用 str1.str2 格式.只对这一个函数的参数进行处理,所有其他代码都应该保持不变.

The arguments can be anything but will always be in a str1.str2 format. It is to be done for arguments of this one function only, all other code should remain untouched.

例如foo(a.x) --> foo(a_x)foo(a4.b6) --> foo(a4_b6)

e.g. foo(a.x) --> foo(a_x) foo(a4.b6) --> foo(a4_b6)

有什么方法可以使用正则表达式或工具来实现它,我可以在一个步骤中对所有文件、一个特定功能执行此操作吗?

推荐答案

如果函数只有一个参数,那就很简单了:

If the function would have only one argument, it would be easy:

使用能够在多个文件中搜索和替换的工具,例如.TextCrawler.

Use a tool that is able to search and replace in multiple files, eg. TextCrawler.

然后选择正则表达式选项卡并填写:

And than select the regular expression tab and fill in:

正则表达式:

(foo\([^)]+)(\.)([^)]+\))

替换:

$1_$3

如果函数中有更多参数,这将不起作用.但是您可以一次又一次地单击替换"按钮,直到它说没有找到结果.您必须最多执行 n 次,其中 n = 任何函数中的最大参数数.

This will not work, if there are more arguments in the function. But you can click the "Replace" button again and then again until it says that no result was found. You will have to do it maximum n-times, where n = max number of arguments in any function.

这篇关于正则表达式在所有文件的函数调用中查找/替换参数模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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