[英]Search and replace text using Regex in IntelliJ
I am using the latest version of the IntelliJ Idea 2017.1 我正在使用最新版本的IntelliJ Idea 2017.1
In my project I have many calls to this function 在我的项目中,我对此函数有很多调用
foo.bar(x) foo.bar(x)
I want to search and replace this to x.toFoo 我想搜索并将其替换为x.toFoo
The problem is that X can be anything. 问题是X可以是任何东西。 it can be any variable, it can be a long function call.
它可以是任何变量,也可以是长函数调用。 So we don't really know the pattern for x.
所以我们真的不知道x的模式。
I tried this regex 我尝试过这个正则表达式
foo.bar(\\S+)
but this doesn't work as it selects many other things. foo.bar(\\S+)
但这不能工作,因为它会选择许多其他内容。
I also tried foo.bar(\\()(.*)(\\))
this seems to select the right thing. 我也尝试了
foo.bar(\\()(.*)(\\))
这似乎选择了正确的东西。 but at the time of replacing. 但在更换时。 how do I remember to capture whatever was there in .*?
我怎么记得捕获。*中的内容?
Example 例
foo.bar(myobj.copy(myid = hisId))
this should become 这应该成为
myobj.copy(myid = hisId).toFoo
another example can be 另一个例子可以是
foo.bar(List(1, 2, 3))
List(1, 2, 3).toFoo
Since your string can have nested parenthesis, I would have suggested using recursion: 由于您的字符串可以带有嵌套的括号,所以我建议使用递归:
foo\.bar(\(([^()]|(?1))*\))
However IntelliJ doesn't support that ( actually haven't seen any IDE supporting recursion ). 但是IntelliJ不支持该功能( 实际上还没有看到任何支持递归的IDE )。
So instead you can use: 因此,您可以使用:
foo\.bar[^\(]*(\(.*\))[^\)]*?
Which given this: 鉴于此:
foo.bar()
foo.bar(1, 2, 3)
foo.bar(1, (2 + (3 + 4)), 5)
You didn't give any example, so I just made some containing nested parenthesis. 您没有提供任何示例,所以我只做了一些包含嵌套括号的示例。
Results in: 结果是:
().toFoo
(1, 2, 3).toFoo
(1, (2 + (3 + 4)), 5).toFoo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.