天天看點

java正則擷取html的圖檔連接配接

public class TestReplaceAll {

	public static void main(String[] args) {
		String str = "<img alt=\"\" src=\"http://img.pppcar.com/image/getImage/57440fef4d5d3925cf12f228/820/0/80\"/>";
		Set<String> pics = getImgStr(str);
		System.out.println(pics);
	}
	
	public static Set<String> getImgStr(String htmlStr) {
        Set<String> pics = new HashSet<>();
        String img = "";
        Pattern p_image;
        Matcher m_image;
        //     String regEx_img = "<img.*src=(.*?)[^>]*?>"; //圖檔連結位址
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile
                (regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            // 得到<img />資料
            img = m_image.group();
            // 比對<img>中的src資料
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                pics.add(m.group(1));
            }
        }
        return pics;
    }
}