作者:手机用户2502916857 | 来源:互联网 | 2022-09-30 19:52
下面小编就为大家带来一篇java正则表达式获取指定HTML标签的指定属性值且替换的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
实例如下:
public static String repDomain(String source, String domain, String element, String attr) {
String img = "";
Pattern p_image;
Matcher m_image;
String regEx_img = "<" + element + "[^<>]*&#63;\\s" + attr + "=['\"]&#63;(.*&#63;)['\"]&#63;(\\s.*&#63;)&#63;>";
p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
m_image = p_image.matcher(source);
while (m_image.find()) {
img = m_image.group();
Matcher m = Pattern.compile("href\\s*=\\s*\"&#63;(.*&#63;)(\"|>|\\s+)").matcher(img);
while (m.find()) {
String srcVal = m.group(1);
if(srcVal.indexOf("/@tenant")>=0){
int idx = srcVal.indexOf("/@tenant");
StringBuffer temp = new StringBuffer();
String dstVal = temp.append(domain).append(srcVal.substring(idx+1)).toString();
source = source.replace(srcVal, dstVal);
}
}
}
return source;
}
以上就是小编为大家带来的java正则表达式获取指定HTML标签的指定属性值且替换的方法全部内容了,希望大家多多支持~