【问题标题】:Regular expression in Java and in Eclipse?Java和Eclipse中的正则表达式?
【发布时间】:2014-01-03 08:01:57
【问题描述】:

我想删除 Java 中的所有空 linse。在 Eclipse 中,我将使用:

\n( *)\n (or "\r\n( *)\r\n" in Windows)

。但在 Java 中它是行不通的(我用过:

str=str.replaceAll("\n( *)\n")

)。如何使用replaceAll在Java中做到这一点?示例:

package example
○○○○
public ... (where ○ is space)

【问题讨论】:

  • 尝试按Ctrl+I 或Ctrl+Shift+F stackoverflow.com/questions/6068009/…
  • replaceAll 接受第二个参数。
  • 但是我有超过 1 000 个文件(我正在编辑大库),所以我不能使用 Ctrl+I 或 +Shift+F 的组合。

标签: java regex eclipse string


【解决方案1】:

我会这样做

java.util.regex.Pattern ws = Pattern.compile("[\r|\n][\\s]*[\r|\n]");
java.util.regex.Matcher matcher = ws.matcher(str);
str = matcher.replaceAll(" ");

【讨论】:

    猜你喜欢
    • 2011-05-31
    • 1970-01-01
    • 2013-03-01
    • 2015-12-29
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    相关资源
    最近更新 更多