在 Eclipse 中用正则表达式替换多行搜索
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16704599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiline search replace with regexp in Eclipse
提问by codegood
Eclipse regexp search works pretty well, so for example in search box I have this:
Eclipse regexp 搜索效果很好,例如在搜索框中我有这个:
(?s)(myMethod.*?;)\}\);
Now I want to copy multiline text in the IDE and in replace box, for example I want to paste \1PASTE_MULTILINE_TEXT_HERE
. However Eclipse does not allow me to directly copy-paste multiline text without manually inserting newline characters.
现在我想在 IDE 和替换框中复制多行文本,例如我想粘贴\1PASTE_MULTILINE_TEXT_HERE
. 但是 Eclipse 不允许我在不手动插入换行符的情况下直接复制粘贴多行文本。
In Vim (Gvim, Macvim) it works perfectly well, keeping all the spaces; how can I do the same thing in Eclipse?
在 Vim (Gvim, Macvim) 中它运行得很好,保留了所有的空间;我怎样才能在 Eclipse 中做同样的事情?
回答by Eugenio
For searching multiple lines in Eclipse, you must use the 's' parameter in search expression:
要在 Eclipse 中搜索多行,您必须在搜索表达式中使用 's' 参数:
(?s)someExpressionToMatchInAnyLine
For replacing with multiple lines exp you must use \R i.e:
要用多行 exp 替换,您必须使用 \R 即:
line1\Rline2\Rline3
This will replace the matched exp with:
line1
line2
line3
这将替换匹配的 exp:
line1
line2
line3
回答by Paul Rowe
Generally, the approach I've taken to doing this sort of thing is to type out what I want to use as a replacement, select that, open up the Find/Replace dialog, and copy the contents of the Find text box. I proceed from there and paste what I copied into the Replace text box. There is still a little work to be done (removing backslashes from in front of regex special characters that don't apply in the Replace box), but it gives me a hand up.
通常,我用来做这种事情的方法是输入我想用作替换的内容,选择它,打开查找/替换对话框,然后复制查找文本框的内容。我从那里开始并将复制的内容粘贴到“替换”文本框中。还有一些工作要做(从在替换框中不适用的正则表达式特殊字符前面删除反斜杠),但它给了我一个帮助。