8

我正在寻找如下替换 java 字符串值。下面的代码不起作用。

        cleanInst.replaceAll("[<i>]", "");
        cleanInst.replaceAll("[</i>]", "");
        cleanInst.replaceAll("[//]", "/");
        cleanInst.replaceAll("[\bPhysics Dept.\b]", "Physics Department");
        cleanInst.replaceAll("[\b/n\b]", ";");
        cleanInst.replaceAll("[\bDEPT\b]", "The Department");
        cleanInst.replaceAll("[\bDEPT.\b]", "The Department");
        cleanInst.replaceAll("[\bThe Dept.\b]", "The Department");
        cleanInst.replaceAll("[\bthe dept.\b]", "The Department");
        cleanInst.replaceAll("[\bThe Dept\b]", "The Department");
        cleanInst.replaceAll("[\bthe dept\b]", "The Department");
        cleanInst.replaceAll("[\bDept.\b]", "The Department");
        cleanInst.replaceAll("[\bdept.\b]", "The Department");
        cleanInst.replaceAll("[\bdept\b]", "The Department");

实现上述替换的最简单方法是什么?

4

3 回答 3

14

如果它是您不断使用的功能,则存在问题。每次调用都会重新编译每个正则表达式。最好将它们创建为常量。你可以有这样的东西。

private static final Pattern[] patterns = {
    Pattern.compile("</?i>"),
    Pattern.compile("//"),
    // Others
};

private static final String[] replacements = {
    "",
    "/",
    // Others
};

public static String cleanString(String str) {
    for (int i = 0; i < patterns.length; i++) {
        str = patterns[i].matcher(str).replaceAll(replacements[i]);
    }
    return str;
}
于 2013-05-31T21:53:41.487 回答
8
cleanInst.replaceAll("[<i>]", "");

应该:

cleanInst = cleanInst.replaceAll("[<i>]", "");

因为String类是不可变的并且不会改变其内部状态,即replaceAll()返回一个不同于cleanInst.

于 2013-05-31T21:14:28.590 回答
3

您应该阅读基本的正则表达式教程

在此之前,您尝试做的事情可以这样完成:

cleanInst = cleanInst.replace("//", "/");
cleanInst = cleanInst.replaceAll("</?i>", "");
cleanInst = cleanInst.replaceAll("/n\\b", ";")
cleanInst = cleanInst.replaceAll("\\bPhysics Dept\\.", "Physics Department");
cleanInst = cleanInst.replaceAll("(?i)\\b(?:the )?dept\\b\\.?", "The Department");

您可能会链接所有这些替换操作(但我不知道正确的 Java 语法)。

关于单词边界\b通常只在字母数字字符之前或之后才有意义。

例如,\b/n\b/n当它的前面直接有一个字母数字字符且后跟一个非字母数字字符时才会匹配,所以它匹配"a/n!"但不匹配"foo /n bar"

于 2013-05-31T21:22:21.213 回答