正则查找一篇文章中的email地址

本文介绍了一个使用Java实现的简单邮件地址爬虫,可以从指定HTML文件中提取所有邮箱地址。此外,还提供了一个代码统计工具,用于统计Java源文件中的正常代码行数、注释行数及空白行数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

查找一片文章中的所有邮箱地址:

public class EmailSpiler {
    public static void main(String[] args) {
        try {
            BufferedReader buf = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\dfsafd.html"));
            String line ="";
            while((line=buf.readLine())!=null){
                parse(line);
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
/**
 * 邮箱的正则
 * @param line
 */
    private static void parse(String line) {
          Pattern p = Pattern.compile("(\\w)+(\\.\\w+)*@((\\w)+(\\.\\w{2,3}){1,3})");
          Matcher m = p.matcher(line);
          while(m.find()){
              System.out.println(m.group());
          }

    }

}

这篇是看你写的代码有多少注释 多少空行 以及实际代码量

public class CodeCounter {
    //正常
    static long normalLine=0;
    //注释行
    static long commentLine =0;
    //空行
    static long whiteLine=0;

    public static void main(String[] args) {
        File f = new File("F:\\新建文件夹 (2)\\shiro02\\src\\main\\java\\com\\wskj\\app\\util\\");
        File[] codeFiles = f.listFiles();
        for (File child : codeFiles) {
            if (child.getName().matches(".*\\.java$")) {
                parse(child);
            }       
        }
        System.out.println("正常代码行数:normalLine="+normalLine);
        System.out.println("注释行数:commentLine="+commentLine);
        System.out.println("空白行数:whiteLine="+whiteLine);
    }
    private static void parse(File  f) {
        BufferedReader br = null;
        boolean comment = false;

        try {
            br = new BufferedReader(new FileReader(f));
            String line="";

            while((line = br.readLine())!=null){
                line = replaceBlank(line);
                if(line.matches("^[\\s&&[^\\n]]*$")){
                    whiteLine ++;
                }else if(line.startsWith("/*") && line.endsWith("*/")){
                    commentLine++;

                }else if(line.startsWith("/*") && !line.endsWith("*/")){
                    commentLine++;
                    comment = true; 
                }else if(true == comment){
                    commentLine++;
                    if(line.endsWith("*/")){
                        comment=false;
                    }
                }else if(line.startsWith("//")){
                    commentLine++;
                }else{
                    normalLine++;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }
    /**
     * 替换所有的空格 制表  换行 为 ""
     * @param str
     * @return
     */
     public static String replaceBlank(String str) {
            String dest = "";
            if (str!=null) {
                Pattern p = Pattern.compile("\\s*|\t|\r|\n");
                Matcher m = p.matcher(str);
                dest = m.replaceAll("");
            }
            return dest;
        }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值