Java 在 Intellij IDEA 中,如何用新行替换文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21219884/
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
In Intellij IDEA how do I replace text with a new line?
提问by Bjorn
Say I wanted to replace all commas with commas and a new line using Intellij IDEA's replace function. What do I put in the search box? In vim I'd use &\r
假设我想使用 Intellij IDEA 的替换功能用逗号和一个新行替换所有逗号。我在搜索框中输入什么?在 vim 中,我会使用 &\r
采纳答案by Andrey Chaschev
You need to check the Regex box and use "\n" for the new line character:
您需要选中 Regex 框并使用 "\n" 作为换行符:
回答by kmera
Hit CTRL+F
and check the regex
checkbox. Then search for ,
and replace it with ,\n
.
点击CTRL+F
并选中regex
复选框。然后搜索,
并替换它,\n
。
回答by pickypg
The easiest way that I have done it is to use the regular expression form of replace.
我所做的最简单的方法是使用正则表达式形式的替换。
Chances are that you don't want to replace the {
, but just keep in my escaping them if you do want to do so.
您可能不想替换{
,但如果您确实想这样做,请继续我的转义。
回答by Meo
Use Multiline button, no Regex is needed.
使用多行按钮,不需要正则表达式。
edit: the multiline button is missing since IntelliJ 15, but you can enable it by clicking into the textfield and pressing Alt+Enter
or Ctrl+Shift+Enter
编辑:自 IntelliJ 15 以来,多行按钮已丢失,但您可以通过单击文本字段并按Alt+Enter
或Ctrl+Shift+Enter
回答by wired00
For those looking for the old multiline replace in inteliJ with version > 15.x. It seems somewhat hidden, but if you select multiple lines > click CTRL+F
, then immediately click CTRL+R
you are presented with the original multiline replace.
对于那些在 inteliJ 中寻找旧的多行替换版本 > 15.x 的人。它似乎有点隐藏,但是如果您选择多行 > 单击CTRL+F
,然后立即单击CTRL+R
您会看到原始的多行替换。
This is working on Mac IntelliJ 2016.1.3
with ?+F > ?+R
这是在 Mac 上IntelliJ 2016.1.3
使用 ?+F > ?+R
回答by spheenik
A clean approach would be to add (?m)
in front of the regular expression, which turns on the multi line mode. This has the advantage that you can also use it in the global file search (Ctrl-Shift-F).
一个干净的方法是(?m)
在正则表达式前面添加,这会打开多行模式。这样做的好处是您还可以在全局文件搜索 (Ctrl-Shift-F) 中使用它。
Example: (?m)\{(.|\n)*?\}
searches for multi-line blocks surrounded by curly braces.
示例:(?m)\{(.|\n)*?\}
搜索由大括号包围的多行块。
回答by Somaiah Kumbera
On intellij Ultimate 2017.1:
在 Intellij Ultimate 2017.1 上:
I didn't need regex. But I could make the multiline replace appear.
我不需要正则表达式。但我可以让多行替换出现。
- I entered \n in the field I wanted to replace
- I placed my cursor in the field where I wanted to enter the replacement text, and clicked Ctrl-Shift + Enter. Here I just hit return
- 我在要替换的字段中输入了 \n
- 我将光标放在要输入替换文本的字段中,然后单击 Ctrl-Shift + Enter。在这里我只是按回车
回答by wiena wu
For Intellij Ultimate 2017.3 on Mac, command-shift-enter
works
对于 Mac 上的 Intellij Ultimate 2017.3,command-shift-enter
有效
回答by Dr Code Monkey
The is related but not exactly what you asked. But I needed it and I can imagine others do to. So I had the problem in Node.js where I wanted to split a reject into call into a log and reject for clarity
是相关的,但不完全是你问的。但我需要它,我可以想象其他人会这样做。所以我在 Node.js 中遇到了问题,为了清楚起见,我想将拒绝拆分为日志并拒绝
reject(error)
reject(error)
into
进入
appLogger.log(error, 'error')
reject(error)
appLogger.log(error, 'error')
reject(error)
In normal mode, I did find and replace
在正常模式下,我确实找到并替换了
Find:
reject(error)
Replace:
appLogger.log(error, 'error')\n reject(error)
找:
reject(error)
代替:
appLogger.log(error, 'error')\n reject(error)
Then in regex mode I did a second find and replace:
然后在正则表达式模式下,我进行了第二次查找和替换:
Find:
\\n
Replace
\n
找:
\\n
代替
\n