改进代码以替换所有字符串

时间:2014-10-15 11:05:23

标签: java string performance replace replaceall

是否有另一种方法可以更有效地使用'replaceAll',尽可能减少内存使用?

 public static String cleanWordTags(String source) {

    String copy = source;

    copy = copy.replaceAll("<P style=\"M[^>]*>", "<P>");
    copy = copy.replaceAll("<p style=\"M[^>]*>", "<p>");
    copy = copy.replaceAll("<p style=\"T[^>]*>", "<p>");

    copy = copy.replaceAll("<b style=[^>]*>", "<b>");

    copy = copy.replaceAll("<span class=\"M[^>]*>", "<span>");
    copy = copy.replaceAll("<span style='m[^>]*>", "<span>");
    copy = copy.replaceAll("<span style=\"f[^>]*>", "<span>");
    copy = copy.replaceAll("<span lang[^>]*>", "<span>");
    copy = copy.replaceAll("<span style=\"color[^>]*>", "<span>");
    copy = copy.replaceAll("<span style=\"m[^>]*>", "<span>");
    copy = copy.replaceAll("<span style=\"line[^>]*>", "<span>");
    copy = copy.replaceAll("<span style=\"L[^>]*>", "<span>");
    copy = copy.replaceAll("<span style=\"T[^>]*>", "<span>");
    copy = copy.replaceAll("<span style=\"t[^>]*>", "<span>");

    copy = copy.replaceAll("<br [^>]*>", "<br/>");

    copy = copy.replaceAll("<i style=[^>]*>", "");
    copy = copy.replaceAll("</i>", "");

    copy = copy.replaceAll("<st1:personname[^>]*>", "");
    copy = copy.replaceAll("</st1:personname>", "");

    copy = copy.replaceAll("<st1:metricconverter[^>]*>", "");
    copy = copy.replaceAll("</st1:metricconverter>", "");

    copy = copy.replaceAll("<br[^>]*>", "<br/>");

    copy = copy.replaceAll("<\\W\\Wendif\\W\\W\\W>", "");

    copy = copy.replaceAll("<![^>]*>", "");


    copy = copy.replaceAll("<[vowm]:[^>]*>", "");
    copy = copy.replaceAll("</[vowm]:[^>]*>", ""); //&

    copy = copy.replaceAll("&(amp|lt|gt);", "");
    copy = copy.replaceAll("&nbsp;", "");

    copy = copy.replaceAll("<img width[^>]*>", "");
    copy = copy.replaceAll("<img src=\"file:[^>]*>", "");


    return copy;
}

我发现我可以使用StringUtils.replace而不是replaceAll,但这仅适用于没有正则表达式的字符串。

感谢!!!

新:

我尝试使用与注释相关的下一个代码,但需要花费5倍的时间来替换相同的字符串:

 public static String cleanWordTags(String source) {
        String copy = source;

        long t0 = System.currentTimeMillis();

        String regex = "";

        regex += "(align=\"left\")";
        regex += "|(<mce:style>)";
        regex += "|(<i>)";
        regex += "|(<i style=[^>]*>)";
        regex += "|(</i>)";
        regex += "|(<st1:personname[^>]*>)";
        regex += "|(</st1:personname>)";
        regex += "|(<st1:metricconverter[^>]*>)";
        regex += "|(</st1:metricconverter>)";
        regex += "|(<\\W\\Wendif\\W\\W\\W>)";
        regex += "|(<![^>]*>)";
        regex += "|(<[vowm]:[^>]*>)";
        regex += "|(</[vowm]:[^>]*>)";
        regex += "|(&(amp|lt|gt);)";
        regex += "|(&nbsp;)";

        regex += "|(<img width[^>]*>)";
        regex += "|(<img src=\"file:[^>]*>)";

        Pattern p = Pattern.compile(regex);
        copy = p.matcher(copy.toUpperCase()).replaceAll("");

        regex = "";
        regex += "(<span style=\"t[^>]*>)";
        regex += "|(<span style=\"T[^>]*>)";
        regex += "|(<span style=\"L[^>]*>)";
        regex += "|(<span style=\"line[^>]*>)";
        regex += "|(<span style=\"m[^>]*>)";
        regex += "|(<span style=\"color[^>]*>)";
        regex += "|(<span lang[^>]*>)";
        regex += "|(<span style=\"f[^>]*>)";
        regex += "|(<span style='m[^>]*>)";
        regex += "|(<span class=\"M[^>]*>)";

        p = Pattern.compile(regex);
        copy = p.matcher(copy.toUpperCase()).replaceAll("");

        copy = copy.replaceAll("<br[^>]*>", "<br/>");

        //Sustituir
        //        copy = copy.replaceAll("<p class=[^>]*>", "<p>");
        //  copy = copy.replaceAll("<p align=[^>]*>", "<p>");
        copy = copy.replaceAll("<P style=\"M[^>]*>", "<P>");
        copy = copy.replaceAll("<p style=\"M[^>]*>", "<p>");
        copy = copy.replaceAll("<p style=\"T[^>]*>", "<p>");
        copy = copy.replaceAll("<b style=[^>]*>", "<b>");

        System.out.println(System.currentTimeMillis() - t0);

        return copy;
    }

3 个答案:

答案 0 :(得分:0)

您是否已经看过 streamflyer (请参阅:https://code.google.com/p/streamflyer/),虽然我无法说明性能,但他们声明:&# 34;修改流中的字符 - 应用正则表达式,修复XML文档,无论你想做什么&#34;

另外还有 streamflyer-regex-fast (参见:https://code.google.com/p/streamflyer-regex-fast/),&#34;提供了一种更快的算法来匹配字符流上的正则表达式而不是streamflyer&#34;

使用的算法

因此,如果您的数据可用Reader,例如作为StringReader,您可以轻松地将首页中的示例应用到您的代码中,如下所示:

Reader reader = new StringReader("source <p style=\"Memphis\">");
FastRegexModifier modifier = new FastRegexModifier("<P style=\"M[^>]*>", Pattern.CASE_INSENSITIVE, "<P>");
ModifyingReader modifyingReader = new ModifyingReader(reader, modifier);
String result = IOUtils.toString(modifyingReader);

这样做的好处是可以使用CASE_INSENSITIVE标志,这可能会减少您需要定义的规则数量。但请注意:这也可能会影响性能,因此您应该评估两种可能性。

请报告,如果此解决方案有助于提升您的表现。

答案 1 :(得分:0)

即使您想使用正则表达式,这种方式效率极低,因为您一次又一次地搜索整个字符串(并创建大量垃圾)。正确的方法是在类似于this one的循环中使用Matcher进行迭代。

让Matcher匹配所有可能感兴趣的内容并分析其发现的内容。您的模式可能类似于

(?:<(p|b|span|br|i|st1:personname|st1:metricconverter|\\W\\Wendif\\W\\W\\W|!|vowm:|img))[^>]+>)|&(amp|lt|gt|nbsp);

匹配的内容超出了您的要求,但在这种情况下您可以将替换设置为$0。它只需要通过整个String传递一次。你可能想做两次通过,以保持简单。

答案 2 :(得分:0)

最后,我找到的唯一解决方案是替换所有“replaceAll”而不使用正则表达式替换“替换”并尝试推广正则表达式。

非常感谢!!!