Java 删除所有空行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4123385/
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
Remove all empty lines
提问by reox
I thought that wasn't that hard to do, but I want to remove all empty lines (or lines just containing blanks and tabs in Java) with String.replaceAll.
我认为这并不难,但我想用 String.replaceAll 删除所有空行(或在 Java 中只包含空格和制表符的行)。
My regex looks like this:
我的正则表达式如下所示:
s = s.replaceAll ("^[ |\t]*\n$", "");
But it doesn't work.
但它不起作用。
I looked around, but only found regexes for removing empty lines without blanks or tabs.
我环顾四周,但只找到了用于删除没有空格或制表符的空行的正则表达式。
采纳答案by Bart Kiers
Try this:
尝试这个:
String text = "line 1\n\nline 3\n\n\nline 5";
String adjusted = text.replaceAll("(?m)^[ \t]*\r?\n", "");
// ...
Note that the regex [ |\t]
matches a space, a tab or a pipe char!
请注意,正则表达式[ |\t]
匹配空格、制表符或管道字符!
EDIT
编辑
B.t.w., the regex (?m)^\s+$
would also do the trick.
顺便说一句,正则表达式(?m)^\s+$
也可以解决问题。
回答by Alin Purcaru
I don't know the syntax for regular expressions in Java, but /^\s*$[\n\r]{1,}/gm
is the regex you're looking for.
我不知道 Java/^\s*$[\n\r]{1,}/gm
中正则表达式的语法,但是您正在寻找的正则表达式。
You probably write it like this in Java:
你可能用 Java 写成这样:
s = s.replaceAll("(?m)^\s*$[\n\r]{1,}", "");
I tested it with JavaScript and it works fine.
我用 JavaScript 测试了它,它工作正常。
回答by zev
I have some code without using regexp, just import org.apache.commons.lang3.StringUtils;
我有一些不使用正则表达式的代码,只需导入 org.apache.commons.lang3.StringUtils;
File temporaire = new File("temp.txt");
try {
Scanner scanner = new Scanner(yourfile);
BufferedWriter bw = new BufferedWriter(new FileWriter(temporaire));
while (scanner.hasNextLine()) {
String line = StringUtils.stripEnd(scanner.nextLine(),null); // Clean blanks at the end of the line
if (StringUtils.isNotBlank(line)) {
bw.write(line); // Keep the line only if not blank
if (scanner.hasNextLine()){
// Go to next line (Win,Mac,Unix) if there is one
bw.write(System.getProperty("line.separator"));
}
}
bw.flush();
}
scanner.close();
bw.close();
fichier.delete();
temporaire.renameTo(fichier);
}
catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
回答by nitesh
If want to remove the lines from Microsoft Office, Windows or an text editor which supports regular expression rendering:
如果要从 Microsoft Office、Windows 或支持正则表达式呈现的文本编辑器中删除这些行:
1. Press <kbd>Ctrl</kbd> + <kbd>F</kbd>.
2. Check the regular expression checkbox
3. Enter Expression ^\s*\n into the find box as it is.
You will see all you black spaces into your editor disappears...
您将看到编辑器中的所有黑色空间都消失了...
回答by Manvendra_0611
You can remove empty lines from your code using the following code:
您可以使用以下代码从代码中删除空行:
String test = plainTextWithEmptyLines.replaceAll("[\\r\\n]+","");
Here, plainTextWithEmptyLines
denotes the string having the empty lines. [\\\r\\\n]
is the regex pattern which is used to identify empty line breaks.
这里,plainTextWithEmptyLines
表示有空行的字符串。[\\\r\\\n]
是用于识别空换行符的正则表达式模式。
回答by morganwahl
I'm not a day-to-day Java programmer, so I'm surprised there isn't a simpler way to do this in the JDK than a regex.
我不是一个日常的 Java 程序员,所以我很惊讶在 JDK 中没有比正则表达式更简单的方法来做到这一点。
Anyway,
反正,
s = s.replaceAll("\n+", "\n");
would be a bit simpler.
会简单一些。
Update:
更新:
Sorry I missed that you wanted to also remove spaces and tabs.
抱歉,我错过了您还想删除空格和制表符。
s = s.replaceAll("\n[ \t]*\n", "\n");
Would work if you have consistent newlines. If not, you may want to consider making them consistent. E.g.:
如果你有一致的换行符,会起作用。如果没有,您可能需要考虑使它们保持一致。例如:
s = s.replaceAll("[\n\r]+", "\n");
s = s.replaceAll("\n[ \t]*\n", "\n");
回答by kriddoff
Bart Kiers's answeris missing the edge case where the last line of the string is empty or contains whitespaces.
Bart Kiers 的回答缺少字符串的最后一行为空或包含空格的边缘情况。
If you try
如果你试试
String text = "line 1\n\nline 3\n\n\nline 5\n "; // <-- Mind the \n plus space at the end!
String adjusted = text.replaceAll("(?m)^[ \t]*\r?\n", "");
you'll get a String that equals this
你会得到一个等于这个的字符串
"line 1\nline 3\nline 5\n " // <-- MIND the \n plus space at the end!
as result.
结果。
I expanded Bart Kiers' answer to also cover this case.
我扩展了Bart Kiers的回答以涵盖这个案例。
My regex pattern is:
我的正则表达式模式是:
String pattern = "(?m)^\s*\r?\n|\r?\n\s*(?!.*\r?\n)";
A little explanation:
一点解释:
The first part of the pattern is basically the same as Bart Kiers'. It is fine, but it does not remove an "empty" last line or a last line containing whitespaces.
模式的第一部分与Bart Kiers'基本相同。这很好,但它不会删除“空”的最后一行或包含空格的最后一行。
That is because a last line containing just whitespaces does not end with \\r?\\n
and would therefore not be matched/replaced. We need something to express this edge case. That's where the second part (after the |
) comes in.
这是因为仅包含空格的最后一行不以 结尾,\\r?\\n
因此不会被匹配/替换。我们需要一些东西来表达这种边缘情况。这就是第二部分(在 之后|
)进来的地方。
It uses a regular expression speciality: negative lookahead. That's the (?!.*\\r?\\n)
part of the pattern. (?!
marks the beginning of the lookahead. You could read it as: Match the regular expression before the lookahead if it is not followed by whatever is defined as string that must not follow. In our case: not any character (zero or more times) followed by a carriage-return (0 or 1 times) and a newline: .*\\r?\\n
. The )
closes the lookahead. The lookahead itself is not part of the match.
它使用正则表达式的特殊性:负前瞻。这是(?!.*\\r?\\n)
模式的一部分。(?!
标志着前瞻的开始。您可以将其读作:如果正则表达式后面没有被定义为不能跟在后面的字符串,则匹配前瞻之前的正则表达式。在我们的例子中:不是任何字符(零次或多次)后跟回车(0 或 1 次)和换行符:.*\\r?\\n
。该)
关闭先行。前瞻本身不是匹配的一部分。
If I execute the following code snippet:
如果我执行以下代码片段:
String pattern = "(?m)^\s*\r?\n|\r?\n\s*(?!.*\r?\n)";
String replacement = "";
String inputString =
"\n" +
"Line 2 - above line is empty without spaces\n" +
"Line 3 - next is empty without whitespaces\n" +
"\n" +
"Line 5 - next line is with whitespaces\n" +
" \n" +
"Line 7 - next 2 lines are \"empty\". First one with whitespaces.\n" +
" \r\n" +
"\n" +
"Line 10 - 3 empty lines follow. The 2nd one with whitespaces in it. One whitespace at the end of this line " +
"\n" +
" \n" +
"\n";
String ajdustedString = inputString.replaceAll(pattern, replacement);
System.out.println("inputString:");
System.out.println("+----");
System.out.println(inputString);
System.out.println("----+");
System.out.println("ajdustedString:");
System.out.println("+----");
System.out.print(ajdustedString); //MIND the "print" instead of "println"
System.out.println("|EOS"); //String to clearly mark the _E_nd _O_f the adjusted_S_tring
System.out.println("----+");
I get:
我得到:
inputString: +---- Line 2 - above line is empty without spaces Line 3 - next is empty without whitespaces Line 5 - next line is with whitespaces Line 7 - next 2 lines are "empty". First one with whitespaces. Line 10 - 3 empty lines follow. The 2nd one with whitespaces in it. One whitespace at the end of this line ----+ ajdustedString: +---- Line 2 - above line is empty without spaces Line 3 - next is empty without whitespaces Line 5 - next line is with whitespaces Line 7 - next 2 lines are "empty". First one with whitespaces. Line 10 - 3 empty lines follow. The 2nd one with whitespaces in it. One whitespace at the end of this line |EOS ----+
If you want to learn more about lookahead/lookbehind see Regex Tutorial - Lookahead and Lookbehind Zero-Length Assertions:
如果您想了解更多关于前瞻/后视的信息,请参阅正则表达式教程 - 前瞻和后视零长度断言: