• Java正则抓取Email



    实现思路:

    1.使用Java.net.URL对象,绑定网络上某一个网页的地址

    2.通过java.net.URL对象的openConnection()方法获得一个HttpConnection对象

    3.通过HttpConnection对象的getInputStream()方法获得该网络文件的输入流对象InputStream

    4.循环读取流中的每一行数据,并由Pattern对象编译的正则表达式区配每一行字符,取得email地址

    关键代码:

    复制代码
    package cn.bdqn;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Test {
        public static void main(String[] args) throws Exception {
            //创建一个url对象
            URL url=new URL("");
            
            //打开连接
            URLConnection conn=url.openConnection();
            
            //设置连接网络超时时间 单位为毫秒
            conn.setConnectTimeout(1000*10);
            
            //通过流操作读取指定网络地址中的文件
            BufferedReader bufr=new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line=null;
            
            //匹配email的正则
            String regex="[a-zA-Z0-9_-]+@\w+\.[a-z]+(\.[a-z]+)?";
            
            //使用模式的compile()方法生成模式对象
            Pattern p=Pattern.compile(regex);
            
            while((line=bufr.readLine())!=null){
                Matcher m=p.matcher(line);
                
                while(m.find()){
                    System.out.println(m.group());
                    
                }
                
            }         
        }
    
    }
    复制代码
  • 相关阅读:
    directshow filter中添加属性页
    (转)3G卡片在开发板上的详细解决方法
    Mean Shift 算法流程
    FloodFill(漫水填充)算法
    Camshift算法(1)
    jQuery click实现toggle(fn,fn)
    每天学点MVC 【Controller返回类型总结】
    jQuery empty在IE下不支持
    Meta标签详解【转载】
    JS格式化
  • 原文地址:https://www.cnblogs.com/hq-123/p/5618023.html
Copyright © 2020-2023  润新知