在Java中读取文件中所有电子邮件


根据题意,我们需要找到文件中存在的所有电子邮件。

让我们探讨一下这篇文章,看看如何使用Java编程语言来实现。

举几个例子

示例1

假设给定一段文字,其中包含许多不同的电子邮件ID,我们想要提取该段文字中存在的所有邮件ID。例如:

“我是一个男孩,我的邮箱是boy54@gmail.com。我朋友的邮箱是friend78@gmail.com。”

从上面的段落中,我们需要提取段落中给定的两个邮件ID,即boy54@gmail.com和friend78@gmail.com。

示例2

假设给定一段文字,其中包含许多不同的电子邮件ID,我们想要提取该段文字中存在的所有邮件ID。例如:

“嘿,我是Rakesh Agarwal,我的邮件ID是rakesh@tutorialspoint.com。我朋友的邮箱是prakash@gmail.com。”

从上面的段落中,我们需要提取段落中给定的两个邮件ID,即rakesh@tutorialspoint.com和prakash@gmail.com。

示例3

假设给定一段文字,其中包含许多不同的电子邮件ID,我们想要提取该段文字中存在的所有邮件ID。例如:

“嘿,我是Rakesh Agarwal,我的邮件ID是rakesh@yahoo.com。

从上面的段落中,我们需要提取段落中给定的邮件ID,即rakesh@yahoo.com。

算法

  • 步骤1 - 导入必要的Java库。

  • 步骤2 - 定义匹配模式的正则表达式。

  • 步骤3 - 指定文本文件的位置或从用户处获取输入。

  • 步骤4 - 使用matcher方法匹配所需格式。

语法

compile() - compile()方法用于多次将文本或表达式与正则表达式匹配。compile()方法用于编译作为字符串传递的给定正则表达式。它属于Pattern类。

matcher() - matcher()方法通过解释Pattern对字符序列执行匹配操作。
readLine() - readLine()方法用于读取给定行文本。它从控制台屏幕获取输入,属于BufferReader类。

多种方法

我们提供了多种解决方案。

  • 通过使用文件输入

  • 通过使用控制台用户输入

让我们逐一查看程序及其输出。

方法1:通过使用文件输入

示例

在这种方法中,我们将从系统本身作为文本文件获取输入,并过滤掉该文本文件中存在的所有电子邮件ID。

import java.util.regex.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { // Regular expression for email id and assigning it to pattern method Pattern pat=Pattern.compile( "[a-zA-Z0-9]" + "[a-zA-Z0-9_.]" + "*@[a-zA-Z0-9]" + "+([.][a-zA-Z]+)+"); //Assigning the location of the text file BufferedReader b = new BufferedReader(new FileReader("E:\file1.txt")); //reading yo.txt file String l = b.readLine(); while (l != null) { //Matching the required format using matcher method Matcher mat = pat.matcher(l); while (mat.find()) { System.out.println(mat.group()); } l = b.readLine(); } } }

输出

abhaprakash@123.com
abhaprakash@tutorialspoint.com
rancho45@gmail.com
y@gmail.com
asifa.sandal120098@yahoo.com
welcometoCodespeedy@hotmail.com.abcxyz
12345@qwerty.asd

方法2:通过使用控制台输入

示例

在这种方法中,我们将从用户处获取输入,并过滤掉其中存在的所有电子邮件ID。

import java.util.regex.*; import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { // Regular expression for email id and assigning it to pattern method Pattern pat=Pattern.compile( "[a-zA-Z0-9]" + "[a-zA-Z0-9_.]" + "*@[a-zA-Z0-9]" + "+([.][a-zA-Z]+)+"); //Taking input from the console Scanner b = new Scanner(System.in); System.out.println("Write the Paragraph containing email ids"); //reading yo.txt file String l = b.nextLine(); System.out.println("\nEmail ids are-"); while (l != null) { //Matching the required format using matcher method Matcher mat = pat.matcher(l); while (mat.find()) { System.out.println(mat.group()); } l = b.nextLine(); } } }

输出

Write the Paragraph containing email ids
abhaprakash@123.com
abhaprakash@com
abhaprakash$gmail.com
abha
abhaprakash@tutorialspoint.com
rancho45@gmail.com
y@gmail.com
asifa.sandal120098@yahoo.com
welcometoCodespeedy@hotmail.com.abcxyz
12345@qwerty.asd

Email ids are-
abhaprakash@123.com
abhaprakash@tutorialspoint.com
rancho45@gmail.com
y@gmail.com
asifa.sandal120098@yahoo.com
welcometoCodespeedy@hotmail.com.abcxyz
12345@qwerty.asd

在这篇文章中,我们探讨了如何在Java中读取文件中所有电子邮件。

更新于:2023年1月11日

222 次浏览

启动你的职业生涯

完成课程获得认证

开始
广告