轉載:http://aguang520.iteye.com/blog/1056686 ...
import java.util.regex.Matcher; import java.util.regex.Pattern; /** * <p> * Title: HTML相關的正則表達式工具類 * </p> * <p> * Description: 包括過濾HTML標記,轉換HTML標記,替換特定HTML標記 * </p> * <p> * Copyright: Copyright (c) 2006 * </p> * * @author hejian * @version 1.0 * @createtime 2006-10-16 */ public class HtmlRegexpUtil { private final static String regxpForHtml = "<([^>]*)>"; // 過濾所有以<開頭以>結尾的標簽 private final static String regxpForImgTag = "<\\s*img\\s+([^>]*)\\s*>"; // 找出IMG標簽 private final static String regxpForImaTagSrcAttrib = "src=\"([^\"]+)\""; // 找出IMG標簽的SRC屬性 /** * */ public HtmlRegexpUtil() { // TODO Auto-generated constructor stub } /** * * 基本功能:替換標記以正常顯示 * <p> * * @param input * @return String */ public String replaceTag(String input) { if (!hasSpecialChars(input)) { return input; } StringBuffer filtered = new StringBuffer(input.length()); char c; for (int i = 0; i <= input.length() - 1; i++) { c = input.charAt(i); switch (c) { case '<': filtered.append("<"); break; case '>': filtered.append(">"); break; case '"': filtered.append("""); break; case '&': filtered.append("&"); break; default: filtered.append(c); } } return (filtered.toString()); } /** * * 基本功能:判斷標記是否存在 * <p> * * @param input * @return boolean */ public boolean hasSpecialChars(String input) { boolean flag = false; if ((input != null) && (input.length() > 0)) { char c; for (int i = 0; i <= input.length() - 1; i++) { c = input.charAt(i); switch (c) { case '>': flag = true; break; case '<': flag = true; break; case '"': flag = true; break; case '&': flag = true; break; } } } return flag; } /** * * 基本功能:過濾所有以"<"開頭以">"結尾的標簽 * <p> * * @param str * @return String */ public static String filterHtml(String str) { Pattern pattern = Pattern.compile(regxpForHtml); Matcher matcher = pattern.matcher(str); StringBuffer sb = new StringBuffer(); boolean result1 = matcher.find(); while (result1) { matcher.appendReplacement(sb, ""); result1 = matcher.find(); } matcher.appendTail(sb); return sb.toString(); } /** * * 基本功能:過濾指定標簽 * <p> * * @param str * @param tag * 指定標簽 * @return String */ public static String fiterHtmlTag(String str, String tag) { String regxp = "<\\s*" + tag + "\\s+([^>]*)\\s*>"; Pattern pattern = Pattern.compile(regxp); Matcher matcher = pattern.matcher(str); StringBuffer sb = new StringBuffer(); boolean result1 = matcher.find(); while (result1) { matcher.appendReplacement(sb, ""); result1 = matcher.find(); } matcher.appendTail(sb); return sb.toString(); } /** * * 基本功能:替換指定的標簽 * <p> * * @param str * @param beforeTag * 要替換的標簽 * @param tagAttrib * 要替換的標簽屬性值 * @param startTag * 新標簽開始標記 * @param endTag * 新標簽結束標記 * @return String * @如:替換img標簽的src屬性值為[img]屬性值[/img] */ public static String replaceHtmlTag(String str, String beforeTag, String tagAttrib, String startTag, String endTag) { String regxpForTag = "<\\s*" + beforeTag + "\\s+([^>]*)\\s*>"; String regxpForTagAttrib = tagAttrib + "=\"([^\"]+)\""; Pattern patternForTag = Pattern.compile(regxpForTag); Pattern patternForAttrib = Pattern.compile(regxpForTagAttrib); Matcher matcherForTag = patternForTag.matcher(str); StringBuffer sb = new StringBuffer(); boolean result = matcherForTag.find(); while (result) { StringBuffer sbreplace = new StringBuffer(); Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag .group(1)); if (matcherForAttrib.find()) { matcherForAttrib.appendReplacement(sbreplace, startTag + matcherForAttrib.group(1) + endTag); } matcherForTag.appendReplacement(sb, sbreplace.toString()); result = matcherForTag.find(); } matcherForTag.appendTail(sb); return sb.toString(); } }
轉載:http://aguang520.iteye.com/blog/1056686