美文网首页
Java正则表达式提取 Email 地址的代码

Java正则表达式提取 Email 地址的代码

作者: jiangmm | 来源:发表于2019-01-05 23:35 被阅读0次

下面的内容是关于Java正则表达式提取 Email 地址的内容,应该是对小伙伴有用。

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

public class RegularExpression {

    public static void main(String[] args) throws IOException {

        Pattern pattern = Pattern.compile("[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}");

        File file = new File("test.txt");

        BufferedReader in = new BufferedReader(new FileReader(file));

        int lines = 0;

        int matches = 0;

        for (String line = in.readLine(); line != null; line = in.readLine()) {

            lines++;

            Matcher matcher = pattern.matcher(line.toUpperCase());

            if (matcher.matches()) {   

                System.out.println(lines + ": '" + line + "'");

                matches++;

            }

        }

        if (matches == 0) {   

            System.out.println("No matches in " + lines + " lines");

        } else {

            System.out.println("n" + matches + " matches in " + lines + " lines");

        }

    }

}

相关文章

网友评论

      本文标题:Java正则表达式提取 Email 地址的代码

      本文链接:https://www.haomeiwen.com/subject/ysrjrqtx.html