JAVA获取指定标签的属性值

package com.zving.teachPlat.util;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.zving.framework.utility.StringUtil;

public class MatchUtil {

    /**
     * 获取指定标签的属性值
     * 
     * @param source: html
     * @param element: 标签
     * @param attr: 标签属性
     */
    public static String match(String source, String element, String attr) {
        String reg = "<" + element + ".*?" + attr + "=['\"]?(.*?)['\"].*?";
        Matcher m = Pattern.compile(reg).matcher(source);
        String r = "";
        while (m.find()) {
            r = m.group(1);
        }
        if (StringUtil.isNotEmpty(r) && r.indexOf("\"") != -1) {
            r = r.substring(0, r.indexOf("\""));
        }
        return r;
    }

}

 

转载于:https://www.cnblogs.com/liuyi-clover/p/9406263.html

你可能感兴趣的:(JAVA获取指定标签的属性值)