【发布时间】:2014-10-15 11:05:23
【问题描述】:
是否有另一种方法可以更有效地执行大量“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(" ", "");
copy = copy.replaceAll("<img width[^>]*>", "");
copy = copy.replaceAll("<img src=\"file:[^>]*>", "");
return copy;
}
我发现我可以使用 StringUtils.replace 代替 replaceAll,但这仅适用于没有正则表达式的字符串。
谢谢!!!
新:
我尝试了下一个与 cmets 相关的代码,但替换相同的字符串需要 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 += "|( )";
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;
}
【问题讨论】:
-
为什么不使用 HTML 解析器之类的?正则表达式 + HTML == 坏主意。
-
问题是我有一个tinymce,人们使用组件的按钮书写或者简单地从单词中复制粘贴,然后将结果用于生成文档,所以我需要控制我自己的标签。
-
我没有解析,我正在删除标签:P
-
您主要关心的是内存消耗还是时间消耗?你的琴弦有多长?
标签: java string performance replace replaceall