Regex to Find/replace argument pattern in a function-call across all files

天大地大妈咪最大 提交于 2020-01-01 19:45:09

问题


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

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

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.

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

Is there any way I can achieve it using regular expression or a tool, where i can do this in one step for all the files, for one specific function?


回答1:


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

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:

RegExp:

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

Replace:

$1_$3

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.



来源:https://stackoverflow.com/questions/3477434/regex-to-find-replace-argument-pattern-in-a-function-call-across-all-files

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