Original: Java HTML string Regular expression using the tool class
Code: http://www.zuidaima.com/share/1550463453416448.htm
HTML-related Regular expression tool classes include filtering HTML tags, converting HTML tags, replacing specific HTML tags
Package Com.zuidaima.common.util;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, converting HTML tags, replacing specific HTML tags * &L t;/p> * <p> * Copyright:copyright (c) * </p> * @author TGL www.zuidaima.com * @version 1.0 * @cre Atetime 2009-10-16 */public class Htmlregexputil {private final static String regxpforhtml = "< ([^>]*] ;"; Filter all tags with < beginning with > end//private final static String Regxpforimgtag = "<\\s*img\\s+ ([^>]*) \\s*>"; Find the IMG tag//private final static String Regxpforimatagsrcattrib = "Src=\" ([^\ "]+) \" "; Find the SRC attribute of the img tag public static void Main (string args[]) {string html = "<DIV>\n<DIV> content feed test \n<div& gt; content feed test \n<div> content feed test \n<div> content feed test \n<div> content feed test \n<div> content feed test \n<div> content feed test </ div></div></div></div></div></div></div></div> "; SYSTEM.OUT.PRINTLN (Replacetag (HTML)); }/** * */Public Htmlregexputil () {//TODO auto-generated constructor stub}/** * Basic function: Replace mark to display normally * <p> * * @param input * @return String */public stat IC 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 ()); }/** * Basic function: Determine if the tag exists * <p> * * @param input * @return Boolean */ public static 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 function: Filter all tags with "<" at the end of ">" * <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 function: Filter the specified label * <p> * * @param str * @param tag * Specify the Mark Sign * @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 function: Replace the specified label * <p> * * @param str * @param beforetag * Label to replace * @param tagattrib * Label property value to replace * @param starttag * NEW tag start tag * @pa Ram Endtag * NEW Tag end tag * @return String * @ For example: replace img tag with src attribute value [img] property value [/IMG] */Public STA Tic 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 (); } }
Java HTML string Regular expression using the tool class