首页 > 解决方案 > 有没有办法将 1 添加到包含 intellij 中查找和替换数字的字符串中?

问题描述

我的代码如下所示:

公共无效 testSomething1() {

}

公共无效 testSomething2() {

}

公共无效 testSomething3() {

}

我希望使用查找和替换的结果看起来像这样:

公共无效 testSomething2() {

}

公共无效 testSomething3() {

}

公共无效 testSomething4() {

我需要这个,因为我刚刚意识到我需要在 2 和 3 之间添加另一个测试并保持其余部分不变。我希望我可以在最后添加它,但我不能,我必须使用这个测试生成工具并为学校项目实施测试。我有 51 个测试,我不想手动移动 n + 1 进行测试,特别是因为有一个评论提到了这个数字。哦,请告诉我有一种方法T_T。谢谢!

标签: intellij-ideareplacestructural-search

解决方案


你可以用Edit | Find | Replace Structurally这个。使用如下模板:

<replaceConfiguration name="Counting" text="void $method$();" recursive="false" type="JAVA" pattern_context="member" reformatAccordingToStyle="false" shortenFQN="false" replacement="void $replacement$();">
  <constraint name="__context__" within="" contains="" />
  <constraint name="method" regexp="(testSomething)(\d+)" within="" contains="" />
  <variableDefinition name="replacement" script="&quot;def (_,name, number) = (method.name =~ /(\p{L}+)(\d+)/)[0]&#10;name + (Integer.parseInt(number) + 1)&#10;&quot;" />
</replaceConfiguration>

您可以复制此模式并使用Import Template from Clipboard结构搜索对话框工具按钮下的菜单中的操作。


推荐阅读