删除所有空行

我认为这并不难,但我想用String.replaceAll删除所有空行(或者只包含Java中的空格和制表符的行)。

我的正则表达式如下:

s = s.replaceAll ("^[ |\t]*\n$", ""); 

但它不起作用。

我环顾四周,但只发现正则表达式删除没有空格或标签的空行。

尝试这个:

 String text = "line 1\n\nline 3\n\n\nline 5"; String adjusted = text.replaceAll("(?m)^[ \t]*\r?\n", ""); // ... 

请注意,正则表达式[ |\t]匹配空格,制表符或管道字符!

编辑

顺便说一句,正则表达式(?m)^\s+$也可以做到这一点。

我不知道Java中正则表达式的语法,但是/^\s*$[\n\r]{1,}/gm是你正在寻找的正则表达式。

您可能在Java中用这样的方式编写它:

 s = s.replaceAll("(?m)^\\s*$[\n\r]{1,}", ""); 

我用JavaScript测试它,它工作正常。

您可以使用以下代码从代码中删除空行:

 String test = plainTextWithEmptyLines.replaceAll("[\\\r\\\n]+",""); 

这里, plainTextWithEmptyLines表示具有空行的字符串。 [\\\r\\\n]是正则表达式模式,用于标识空换行符。

我不是一个日常的Java程序员,所以我很惊讶JDK中没有比正则表达式更简单的方法。

无论如何,

 s = s.replaceAll("\n+", "\n"); 

会有点简单。

更新:

对不起,我想念你也想删除空格和标签。

 s = s.replaceAll("\n[ \t]*\n", "\n"); 

如果您有一致的换行符会有效。 如果没有,您可能需要考虑使它们保持一致。 例如:

 s = s.replaceAll("[\n\r]+", "\n"); s = s.replaceAll("\n[ \t]*\n", "\n"); 

我有一些代码没有使用regexp,只需导入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()); } } 

如果要从Microsoft Office,Windows或支持正则表达式呈现的文本编辑器中删除行:

  1. Press Ctrl + F. 2. Check the regular expression checkbox 3. Enter Expression ^\s*\n into the find box as it is. 

您将看到编辑器中的所有黑色空格消失…

Bart Kiers的答案是缺少边缘情况,其中字符串的最后一行是空的或包含空格。

如果你试试

 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", ""); 

你会得到一个等于此的字符串

 "line 1\nline 3\nline 5\n " // <-- MIND the \n plus space at the end! 

结果。

我扩大了Bart Kiers的答案,也涵盖了这个案例。

我的正则表达式是:

 String pattern = "(?m)^\\s*\\r?\\n|\\r?\\n\\s*(?!.*\\r?\\n)"; 

一点解释:

该模式的第一部分与Bart Kiers的基本相同。 没关系,但它不会删除“空”的最后一行或包含空格的最后一行。

这是因为包含空格的最后一行不以\\r?\\n结尾,因此不会匹配/替换。 我们需要一些东西来表达这种边缘情况。 那就是第二部分(在| )。

它使用正则表达式专业: 负向前瞻 。 这是模式的(?!.*\\r?\\n)部分。 (?!标记了前瞻的开头。你可以把它读作:在前瞻之前匹配正则表达式,如果它没有跟随被定义为不能跟随的字符串的任何东西。在我们的例子中:不是任何字符(零或更多)次)然后回车(0或1次)和换行符: .*\\r?\\n)关闭前瞻。 前瞻本身不是比赛的一部分。

如果我执行以下代码片段:

 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("----+"); 

我明白了:

 inputString:
 + ----

第2行 - 上面的行是空的,没有空格
第3行 -  next是空的,没有空格

第5行 - 下一行是空格

第7行 - 接下来的2行是“空”。 第一个有空格。


第10行 - 第3行空行。 第二个有空格。 这一行末尾有一个空格



 ---- +
 ajdustedString:
 + ----
第2行 - 上面的行是空的,没有空格
第3行 -  next是空的,没有空格
第5行 - 下一行是空格
第7行 - 接下来的2行是“空”。 第一个有空格。
第10行 - 第3行空行。 第二个有空格。 这一行末尾的一个空格| EOS
 ---- +

如果你想了解更多关于lookahead / lookbehind的信息,请参阅Regex Tutorial - Lookahead和Lookbehind Zero-Length Assertions: