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

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格式的,如需匹配其他格式同理。

posted @ 2016-02-02 18:32  爱茹一婉年  阅读(2719)  评论(0编辑  收藏  举报