java过滤html注释,java正则表达式过滤html标签import Matcher;
import Pattern;
/**
* Title: HTML相关的正则表达式⼯具类
* Description: 包括过滤HTML标记,转换HTML标记,替换特定HTML标记
* Copyright: Copyright (c) 2006
*
* @authorhejian
* @version 1.0
* @createtime 2006-10-16
*/
public classHtmlRegexpUtil {
privatefinalstaticStringregxpForHtml= "]*)>"; // 过滤所有以结尾的标签
privatefinalstaticStringregxpForImgTag= "]*)\\s*>"; // 出IMG标签
privatefinalstaticStringregxpForImaTagSrcAttrib = "src=\"([^\"]+)\""; // 出IMG标签的SRC属性
publicHtmlRegexpUtil() {
}
/**
* 基本功能:替换标记以正常显⽰
* @param input
* @return String
*/
publicString replaceTag(String input) {
if (!hasSpecialChars(input)) {
return input;
}
StringBuffer filtered = newStringBuffer(input.length());
char c;
for (int i = 0; i <= input.length() - 1; i++){
c = input.charAt(i);
switch (c) {
case'
filtered.append("
case'>':
filtered.append(">");
break;
case'"':
filtered.append(""");
break;
case'&':
filtered.append("&");
break;
default:
filtered.append(c);
}
}
return (String());
}
/**
* 基本功能:判断标记是否存在
* @param input
* @return boolean
*/
publicbooleanhasSpecialChars(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'
java修改html文件flag = true;
break;
flag = true;
break;
case'&':
flag = true;
break;
}
}
}
return flag;
}
/**
* 基本功能:过滤所有以""结尾的标签
* @param str
* @return String
*/
publicstaticString filterHtml(String str) {
Pattern pattern = Patternpile(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);
String();
}
/**
* 基本功能:过滤指定标签
* @param str
* @param tag
* 指定标签
* @return String
*/
publicstaticString fiterHtmlTag(String str, String tag) {
String regxp = "]*)\\s*>";
Pattern pattern = Patternpile(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);
String();
}
/**
* 基本功能:替换指定的标签
* @param str
* @param beforeTag
* 要替换的标签
* @param tagAttrib
* 要替换的标签属性值
* @param startTag
* 新标签开始标记
* @param endTag
* 新标签结束标记
* @return String
* @如:替换img标签的src属性值为[img]属性值[/img]
*/
publicstaticString replaceHtmlTag(String str, String beforeTag, String tagAttrib, String startTag,String endTag) {
String regxpForTag = "]*)\\s*>";
String regxpForTagAttrib = tagAttrib + "=\"([^\"]+)\"";
Pattern patternForTag = Patternpile(regxpForTag);
Pattern patternForAttrib = Patternpile(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
+up(1) + endTag);
}
matcherForTag.appendReplacement(String());
result = matcherForTag.find();
}
matcherForTag.appendTail(sb);
String();
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。