使用Java String.replaceAll的正则表达式

使用Java String.replaceAll的正则表达式,java,regex,string,replaceall,Java,Regex,String,Replaceall,我希望如下替换java字符串值。下面的代码不工作 cleanInst.replaceAll("[<i>]", ""); cleanInst.replaceAll("[</i>]", ""); cleanInst.replaceAll("[//]", "/"); cleanInst.replaceAll("[\bPhysics Dept.\b]", "Physics Department");

我希望如下替换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");
cleanInst.replaceAll(“[]”,“”);
cleanInst.replaceAll(“[]”,“”);
cleanInst.replaceAll(“[/]”,“/”);
cleanInst.replaceAll(“[\b物理系].”,“物理系”);
cleanInst.replaceAll(“[\b/n\b]”,“;”);
cleanInst.replaceAll(“[\bDEPT\b]”,即“部门”);
cleanInst.replaceAll(“[\bDEPT.\b]”,即“部门”);
cleanInst.replaceAll(“[\b部门]”,“部门”);
cleanInst.replaceAll(“[\b部门]”,“部门”);
cleanInst.replaceAll(“[\b部门]”,“部门”);
cleanInst.replaceAll(“[\b部门]”,“部门”);
cleanInst.replaceAll(“[\bDept.\b]”,即“部门”);
cleanInst.replaceAll(“[\bdept.\b]”,即“部门”);
cleanInst.replaceAll(“[\bdept\b]”,即“部门”);
实现上述替换的最简单方法是什么?

cleanInst.replaceAll(“[]”,“);
cleanInst.replaceAll("[<i>]", "");
应该是:

cleanInst = cleanInst.replaceAll("[<i>]", "");
cleanInst=cleanInst.replaceAll(“[]”,“”);
由于
String
类是不可变的,并且不会更改其内部状态,即
replaceAll()
返回一个不同于
cleanInst

的新实例,因此您应该阅读一个basic

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

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");
cleanInst=cleanInst.replace(“//”,“/”);
cleanInst=cleanInst.replaceAll(“,”);
cleanInst=cleanInst.replaceAll(“/n\\b”,“;”)
cleanInst=cleanInst.replaceAll(\\b物理系\\.,“物理系”);
cleanInst=cleanInst.replaceAll(“(?i)\\b(?:部门\\ b\\?”,“部门”);
您可能会链接所有这些替换操作(但我不知道用于此操作的正确Java语法)

关于:
\b
通常仅在字母数字字符之前或之后才有意义


例如,
\b/n\b
只有在前面直接跟一个字母数字字符,后面跟一个非字母数字字符的情况下才会匹配
/n
,因此它匹配
“a/n!”
,但不匹配
“foo/n bar”

,如果它是一个您一直在使用的函数,则存在问题。对于每个调用,将再次编译每个正则表达式。最好将它们创建为常量。你可以吃这样的东西

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;
}
private静态最终模式[]模式={
Pattern.compile(“”),
Pattern.compile(“/”),
//其他
};
私有静态最终字符串[]替换={
"",
"/",
//其他
};
公共静态字符串cleanString(字符串str){
for(int i=0;i
不工作是什么意思?请删除方括号(
[
]
)。这些是用于角色类的。若其他东西不起作用,你们需要更具体一些。字符串是不可变的。而忽略大小写修饰符将适用于许多
dept
replacesAs@SLaks已经指出:字符串是不可变的。如果不将
String.replaceAll()
的返回值存储在某个地方,代码将不会执行任何操作。现在,您的代码对返回值没有任何影响。+1您的答案很好,但为什么非捕获组为“the”?只是“表演”吗?Cos IMHO可读性的下降大于性能的提高。顺便说一句,我怀疑
/n
应该是
\n
我只是习惯了这样做。我从不使用捕获括号,除非我想捕获一个组。我同意清楚地表达自己的意图和可读性之间存在着紧张关系。我们现在不再使用
模式
,而是每次都创建
匹配器
对象。这有什么好处?因为编译正则表达式模式比为(预编译的)模式创建匹配器更昂贵?