首页 > 编程语言 > 详细

Java实现正则匹配HTML中获取的图片链接

时间:2016-02-02 18:59:59      阅读:338      评论:0      收藏:0      [点我收藏+]
package demo;

import java.io.File;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo {
    
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        String content="fdsafewafdsfasd<img alt=\"\" src=\"http://lady.people.com.cn/NMediaFile/2015/1012/MAIN201510120940000465871834275.jpg\" width=\"500\" height=\"333\" style=\"border: none;\">fffweaer"
            +"<img alt=\"\" src=\"http://lady.people.com.cn/NMediaFile/2015/1012/MAIN201510120941000081018729633.JPG\" width=\"500\" height=\"750\" style=\"border: none;\">fawefdsf"
            +"<img alt=\"\" src=\"http://lady.people.com.cn/NMediaFile/2015/1012/MAIN201510120941000081018729633.html\" width=\"500\" height=\"750\" style=\"border: none;\">43243543"
            +"<img alt=\"\" src=\"/viscms/u/cms/rzw/201510/12105640pvgz.jpg\" width=\"500\" height=\"333\" style=\"border: none;\"/>asdfaewfaghf"
            +"<img alt=\"\" src=\"http://lady.people.com.cn/NMediaFile/2015/1012/MAIN201510120941000230259847261.jpg\" width=\"500\" height=\"333\" style=\"border: none;\">fasdfaf";
        match(content);

    }
    
    public static void match(String content) {
        Matcher m = Pattern.compile("src=\"http://.*?\"").matcher(content);
        while(m.find()){
            String match=m.group();
       //Pattern.CASE_INSENSITIVE忽略‘jpg‘的大小写 Matcher k
=Pattern.compile("src=\"http://.*?.jpg",Pattern.CASE_INSENSITIVE).matcher(match); if(k.find()){ System.out.println(match); } } } }

PS:这段代码可以用于采集HTML网页中的图片链接匹配,本段代码匹配的图片是jpg格式的,如需匹配其他格式同理。

Java实现正则匹配HTML中获取的图片链接

原文:http://www.cnblogs.com/wanying521/p/5178299.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!