Java Regular Expression filters html tags and java Regular Expression tags
Import java. util. regex. matcher; import java. util. regex. pattern;/***** <p> * Title: HTML-related regular expression tool class * </p> * <p> * Description: includes filtering HTML tags and converting HTML tags, replace a specific HTML Tag * </p> * <p> * Copyright: Copyright (c) 2006 * </p> ** @ author hejian * @ version 1.0 * @ createtime 2006-10-16 */public class HtmlRegexpUtil {private final static String regxpForHtml = "<([^>] *)> "; // filter all private final static Str tags whose names start with <and end with> Ing regxpForImgTag = "<\ s * img \ s + ([^>] *) \ s *> "; // find the IMG label private final static String regxpForImaTagSrcAttrib = "src = \" ([^ \ "] + )\""; // find out the SRC attribute of the IMG tag/*****/public HtmlRegexpUtil () {// TODO Auto-generated constructor stub}/***** basic functions: replace the flag to display normally * <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 ("& lt;"); break; case '>': filtered. append ("& gt;"); break; case '"': filtered. append ("& quot;"); break; case '&': filtered. append ("& amp;"); break; default: filtered. append (c) ;}} return (Filtered. toString ();}/*** basic function: determines whether a tag exists * <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;}/***** basic functions: filter all tags whose names start with "<" and end with ">" * <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 ();}/***** basic functions: filter the specified tag * <p> ** @ param str * @ param tag * specify the 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 ();}/***** basic functions: replace the specified tag * <p> ** @ param str * @ param beforeTag * The tag to be replaced * @ param tagAttrib * The tag attribute value to be replaced * @ param startTag * The new tag begins. mark * @ param endTag * new tag end tag * @ return String * @ for example: replace the src attribute value of the img tag with the [img] attribute value [/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 ();}}
Reprinted: http://aguang520.iteye.com/blog/1056686