網路爬蟲之抓取郵箱

lambdaλ發表於2018-06-18
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 抓取郵箱號碼
 */
public class Demo7 {
    public static void main(String[] args) {
        String str = "有事沒事聯絡:1122423@qq.com 有事沒事聯絡:1122423@qq.com 有事沒事聯絡:1122423@qq.com "
                + "有事沒事聯絡:1122423@qq.com 有事沒事聯絡:1122423@qq.com 有事沒事聯絡:1122423@qq.com"
                + "有事沒事聯絡:1122423@qq.com 有事沒事聯絡:1122423@qq.com.cn 有事沒事聯:1122423@qq.com.cn"
                + "有事沒事聯絡:1122423@163.com 有事沒事聯絡:1122423@qq.net";
        String reg = "[a-zA-Z1-9]\w{1,11}@[a-zA-Z0-9]{2,}(\.[a-z]{2,3}){1,2}";
        /*
         第一步:
            先要把字串的正則編譯成Pattern物件
        */
        Pattern p = Pattern.compile(reg);
        /*
         第二步:
            把正則物件匹配字串物件得到一個匹配器
         */
        Matcher m = p.matcher(str);
        while(m.find()){
            System.out.println(m.group());
        }
    }
}


相關文章