获取网页上的邮箱

import java.io.*;
import java.util.regex.*;
import java.net.*;
class RegexTest
{
    public static void main(String[] args)throws Exception
    {
        getMails();
    }
    
    public static void getMails() throws Exception
    {
        String reg = "\\w+@\\w+(\\.\\w+)+";//正则表达式匹配邮箱
        Pattern p = Pattern.compile(reg);//将正则表达式封装成对象
        URL url = new URL("http://tieba.baidu.com/p/3460373556");//创建url对象,数据源
        URLConnection conn = url.openConnection();//持续连接
        BufferedReader bufrIn = new BufferedReader(new InputStreamReader(conn.getInputStream()));//获取输入流
        String line = null;
        while((line=bufrIn.readLine())!=null)
        {
            Matcher m = p.matcher(line);//将字符串与正则表达式相关联
            while(m.find())
            {
                System.out.println(m.group());
            }
        }
        
    }
}

 

posted @ 2016-02-23 13:13  lovedaydream  阅读(365)  评论(0编辑  收藏  举报