HTML转纯文本,求吐槽

首先吐槽一下iteye的问卷。
然后,闲的蛋疼做了一个HTML转纯文本的小工具类。
当然功能上只保留了一些最简单的例如<br>,<p>,<div>把它们转换成\n。


package com.yyt.core.utils.net.ubb;

import java.util.regex.Pattern;

public class HTMLUtils {

	private static final Pattern HTML_CHECKER = Pattern
			.compile("</[a-z][^>]*>|<[a-z][^/>]*/>");

	public static String toText(String html) {

		if (html == null)
			return null;

		boolean is = isHtml(html);

		if (is) {
			html = html
					.replaceAll("\r?\n", "")
					.replaceAll(
							"<[sS][tT][yY][lL][eE] [^>]+>[\\d\\D]+?</[sS][tT][yY][lL][eE]>",
							"")
					.replaceAll(
							"<[sS][tT][yY][lL][eE]>[\\d\\D]+?</[sS][tT][yY][lL][eE]>",
							"")
					.replaceAll(
							"<[sS][cC][rR][iI][pP][tT] [^>]+>[\\d\\D]+?</[sS][cC][rR][iI][pP][tT]>",
							"")
					.replaceAll(
							"<[sS][cC][rR][iI][pP][tT]>[\\d\\D]+?</[sS][cC][rR][iI][pP][tT]>",
							"")
					.replaceAll(
							"<[tT][aA][bB][lL][eE] [^>]+>[\\d\\D]+?</[tT][aA][bB][lL][eE]>",
							"")
					.replaceAll(
							"<[tT][aA][bB][lL][eE]>[\\d\\D]+?</[tT][aA][bB][lL][eE]>",
							"")
					.replaceAll("\n*(<(?!\\s)[^>\n]+>)\n*", "$1")
					.replaceAll(
							"(</?[bB][rR]\\s*+/?[^>]*+>|</?[pP][^>]*+>|</[dD][iI][vV][^>]*+>|</[lL][iI][^>]*+>)\\s*+",
							"\n");
		}

		return html = html.replaceAll("</?(?!\\s)[a-zA-Z][^>]*>", "")
				.replaceAll("<!--[\\d\\D]*?-->", "").replaceAll("\n{2,}", "\n");

	}

	public static boolean isHtml(String html) {
		return HTML_CHECKER.matcher(html).find();
	}
}


啊咧,有没有发错版块呢?

你可能感兴趣的:(java,regex)