/**
* 替换指定标签的属性和值
* @param str 需要处理的字符串
* @param tag 标签名称
* @param tagAttrib 要替换的标签属性值
* @param startTag 新标签开始标记
* @param endTag 新标签结束标记
* @return
*/
public static String replaceHtmlTag(String str, String tag, String tagAttrib, String startTag, String endTag) {
String regxpForTag = "<\\s*" + tag + "\\s+([^>]*)\\s*" ;
String regxpForTagAttrib = tagAttrib + "=\\s*\"([^\"]+)\"" ;
Pattern patternForTag = Pattern.compile (regxpForTag,Pattern.CASE_INSENSITIVE );
Pattern patternForAttrib = Pattern.compile (regxpForTagAttrib,Pattern.CASE_INSENSITIVE );
Matcher matcherForTag = patternForTag.matcher(str);
StringBuffer sb = new StringBuffer();
boolean result = matcherForTag.find();
while (result) {
StringBuffer sbreplace = new StringBuffer( "<"+tag+" ");
Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag.group(1));
if (matcherForAttrib.find()) {
String attributeStr = matcherForAttrib.group(1);
//attributeStr = attributeStr.replaceAll("\\{","").replaceAll("\\}","");
matcherForAttrib.appendReplacement(sbreplace, startTag + attributeStr + endTag);
}
matcherForAttrib.appendTail(sbreplace);
matcherForTag.appendReplacement(sb, sbreplace.toString());
result = matcherForTag.find();
}
matcherForTag.appendTail(sb);
return sb.toString();
}
Java 正则替换指定标签的属性和值
最新推荐文章于 2023-04-24 17:31:23 发布