在Intellij中查找并替换正则表达式,但是保留一些匹配的正则表达式吗? [英] Find and replace regex in Intellij, but keep some of the matched regex?
问题描述
我将数组更改为列表,所以我想将 myObject [index]
的所有实例更改为 myObject.get(index)
,其中index是不同的整数.我可以通过找到这些实例
I changed an array to a list, so I want to change all instances of myObject[index]
to myObject.get(index)
where index is different integers. I can find these instances by doing
`myObject\[.*\]`
但是,我不确定应该在替换行中添加什么-我不知道如何使它保持 index
值.
However, I am not sure what I should put in the replace line - I don't know how to make it keep the index
values.
推荐答案
使用以下正则表达式替换:
Use the following regex replacement:
查找: myObject \ [(.*?)\]
替换: myObject.get($ 1)
如果 index
是整数,则可以将(.*?)
替换为(\ d +)
.
If the index
is an integer, you may replace (.*?)
with (\d+)
.
一对未转义的括号会创建一个捕获组,我们可以使用 $
+组ID从替换模式中引用该捕获组. $ 1
会将索引插入替换结果.
The pair of unescaped parentheses creates a capturing group that we may reference from the replacement pattern using $
+ Group ID. $1
will insert the index into the replacement result.
这篇关于在Intellij中查找并替换正则表达式,但是保留一些匹配的正则表达式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!