使用 eclipse 查找并替换全部来交换参数
我有大约 100 行,如下所示:
assertEquals(results.get(0).getID(),1);
它们都以assertEquals 开头并包含两个参数。我正在寻找一种使用查找和替换所有来交换所有这些行的参数的方法。
谢谢
I have about 100 lines that look like the below:
assertEquals(results.get(0).getID(),1);
They all start with assertEquals and contain two arguments. Im looking for a way to use find and replace all to swap the arguments of all these lines.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您发现自己经常在方法声明中交换参数顺序,我找到了一个插件,只需单击一下即可为您完成此操作。
该插件向 Eclipse Java 编辑器添加了两个工具栏按钮:
插入
符号位于 |在:
单击“向前交换”按钮会产生:
或单击“向后交换”按钮与原始源会产生:
这里是讨论它的文章。
这里是 jar放入您的 Eclipse 插件目录中。
If you find yourself swapping parameter order in method declarations farily often, I found a plugin that does it for you with a single click.
This plug-in adds two toolbar buttons to the Eclipse Java editor:
With the caret at | in:
clicking the Swap forward button yields:
or clicking Swap backward button with the original source yields:
Here is the article discussing it.
Here is the jar to drop into your eclipse plugin directory.
您还可以使用 Eclipse 的内置方法签名重构来重新排序参数。
在从 JUnit 转换为 TestNG 的情况下(这就是您正在做的事情),您可以将 org.testng.Assert 复制到您的项目中并重构assertXYZ 方法以转置预期/实际参数。
完成后,删除 org.testng.Assert 的副本
You can also use Eclipse's built-in method signature refactoring to re-order the arguments.
In the case of converting from JUnit to TestNG (which is what it looks like you're doing), you can copy org.testng.Assert into your project and refactor the assertXYZ methods to transpose the expected/actual arguments.
When you're done, delete your copy of org.testng.Assert
搜索
assertEquals\((.*),\s*(.*)\);
并替换为
assertEquals(\2, \1);
抱歉,更多或者更少多余的答案。但它确实对我来说效果更好,我认为对其他人也是如此。
我对第一篇文章的编辑被拒绝了。
search for
assertEquals\((.*),\s*(.*)\);
and replace with
assertEquals(\2, \1);
Sorry for the more or less superflous answer. But it does work better for me and I think for others as well.
My edit of the first post was rejected.
使用以下正则表达式查找:
和此替换值:
正则表达式的意思是“assertEquals( 后跟第一组字符,后跟逗号,后跟第二组字符,后跟 );”。
替换值的意思是“assertEquals( 后跟找到的第二组字符,后跟逗号,后跟找到的第一组字符,后跟 );”。
use the following regexp to find:
and this replacement value:
The regexp means "assertEquals( followed by a first group of chars followed by a comma followed by a second group of chars followed by );".
The replacement value means "assertEquals( followed by the second group of chars found followed by a comma followed by the first group of chars found followed by );".
我不知道如何在 Eclipse 中执行此操作,但如果您碰巧还安装了
vim
,您可以在其中加载文件并执行I don't know how to do it in Eclipse, but if you happen to also have a
vim
installed you might load your file in it and do