Find and replace regex in Intellij, but keep some of the matched regex?

99封情书 提交于 2021-01-18 02:48:45

问题


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\[.*\]`

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.


回答1:


Use the following regex replacement:

Find: myObject\[(.*?)\]
Replace: myObject.get($1)

If the index is an integer, you may replace (.*?) with (\d+).

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.



来源:https://stackoverflow.com/questions/40537987/find-and-replace-regex-in-intellij-but-keep-some-of-the-matched-regex

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