有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java中的正则表达式匹配和替换

我有一个有很多行的文件。中间可能有空行。我想匹配所有具有特定模式且后跟空换行符的行,并将其替换为仅包含该行(不包含以下空换行符)。没有图案但后面仍有空行的线条应保持原样

样本文件

a + b --> c

c + d --> e
The empty line after this is left alone

e + a --> b

a --> b + c

输出文件

a + b --> c
c + d --> e
The empty line after this is left alone

e + a --> b
a --> b + c

我有一个匹配所有这些线条的图案

String linePattern = "(.*-->.*)(\n\n)";
Pattern compiledPattern = Pattern.compile(linePattern);
Matcher matcher = compiledPattern.matcher(fileContentsAsString);

有没有一种优雅的方法可以从整个字符串中去掉这些行后面多余的空行


共 (0) 个答案