源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

java正则表达式获取指定HTML标签的指定属性值且替换的方法

  • 时间:2020-08-06 14:42 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:java正则表达式获取指定HTML标签的指定属性值且替换的方法
[b]实例如下:[/b]
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标签的指定属性值且替换的方法全部内容了,希望大家多多支持编程素材网~
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部