当前位置: 首页 > 工具软件 > chtml-matcher > 使用案例 >

java正则 过滤html标签_java正则表达式过滤html标签

常茂
2023-12-01

importjava.util.regex.Matcher;importjava.util.regex.Pattern;/***

* Title: HTML相关的正则表达式工具类

*

*

* Description: 包括过滤HTML标记,转换HTML标记,替换特定HTML标记

*

*

* Copyright: Copyright (c) 2006

*

*

*@authorhejian

*@version1.0

* @createtime 2006-10-16*/

public classHtmlRegexpUtil {private final static String regxpForHtml = "]*)>"; //过滤所有以结尾的标签

private final static String regxpForImgTag = "]*)\\s*>"; //找出IMG标签

private final static String regxpForImaTagSrcAttrib = "src=\"([^\"]+)\""; //找出IMG标签的SRC属性

/****/

publicHtmlRegexpUtil() {//TODO Auto-generated constructor stub

}/***

* 基本功能:替换标记以正常显示

*

*

*@paraminput

*@returnString*/

publicString replaceTag(String input) {if (!hasSpecialChars(input)) {returninput;

}

StringBuffer filtered= newStringBuffer(input.length());charc;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());

}/***

* 基本功能:判断标记是否存在

*

*

*@paraminput

*@returnboolean*/

public booleanhasSpecialChars(String input) {boolean flag = false;if ((input != null) && (input.length() > 0)) {charc;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;

}

}

}returnflag;

}/***

* 基本功能:过滤所有以""结尾的标签

*

*

*@paramstr

*@returnString*/

public staticString filterHtml(String str) {

Pattern pattern=Pattern.compile(regxpForHtml);

Matcher matcher=pattern.matcher(str);

StringBuffer sb= newStringBuffer();boolean result1 =matcher.find();while(result1) {

matcher.appendReplacement(sb,"");

result1=matcher.find();

}

matcher.appendTail(sb);returnsb.toString();

}/***

* 基本功能:过滤指定标签

*

*

*@paramstr

*@paramtag

* 指定标签

*@returnString*/

public staticString fiterHtmlTag(String str, String tag) {

String regxp= "]*)\\s*>";

Pattern pattern=Pattern.compile(regxp);

Matcher matcher=pattern.matcher(str);

StringBuffer sb= newStringBuffer();boolean result1 =matcher.find();while(result1) {

matcher.appendReplacement(sb,"");

result1=matcher.find();

}

matcher.appendTail(sb);returnsb.toString();

}/***

* 基本功能:替换指定的标签

*

*

*@paramstr

*@parambeforeTag

* 要替换的标签

*@paramtagAttrib

* 要替换的标签属性值

*@paramstartTag

* 新标签开始标记

*@paramendTag

* 新标签结束标记

*@returnString

* @如:替换img标签的src属性值为[img]属性值[/img]*/

public staticString replaceHtmlTag(String str, String beforeTag,

String tagAttrib, String startTag, String endTag) {

String regxpForTag= "]*)\\s*>";

String regxpForTagAttrib= tagAttrib + "=\"([^\"]+)\"";

Pattern patternForTag=Pattern.compile(regxpForTag);

Pattern patternForAttrib=Pattern.compile(regxpForTagAttrib);

Matcher matcherForTag=patternForTag.matcher(str);

StringBuffer sb= newStringBuffer();boolean result =matcherForTag.find();while(result) {

StringBuffer sbreplace= newStringBuffer();

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);returnsb.toString();

}

}

 类似资料: