java正则表达式获取指定HTML标签的指定属性值且替换的方法
程序员文章站
2024-03-09 08:42:11
实例如下:
public static string repdomain(string source, string domain, string element...
实例如下:
public static string repdomain(string source, string domain, string element, string attr) { string img = ""; pattern p_image; matcher m_image; string regex_img = "<" + element + "[^<>]*?\\s" + attr + "=['\"]?(.*?)['\"]?(\\s.*?)?>"; 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*\"?(.*?)(\"|>|\\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标签的指定属性值且替换的方法全部内容了,希望大家多多支持~