Home > Java > javaTutorial > Java regular expression method to obtain and replace the specified attribute value of a specified HTML tag

Java regular expression method to obtain and replace the specified attribute value of a specified HTML tag

高洛峰
Release: 2017-01-22 14:50:56
Original
1579 people have browsed it

实例如下:

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 + "=[&#39;\"]?(.*?)[&#39;\"]?(\\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;
  }
Copy after login

   

以上就是小编为大家带来的java正则表达式获取指定HTML标签的指定属性值且替换的方法全部内容了,希望大家多多支持脚PHP中文网~

更多java正则表达式获取指定HTML标签的指定属性值且替换的方法相关文章请关注PHP中文网!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template