在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,我的邮箱是rakesh@yahoo.com。
从上述段落中,我们需要取出该段中给定的邮件ID,即:rakesh@yahoo.com。
算法
- 第1步 – 导入必要的java库。
-
第2步 – 定义匹配模式的正则表达式。
-
第3步 – 指定文本文件的位置或接受用户的输入。
-
第4步 – 使用匹配器方法匹配所需的格式。
语法
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("
Email 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读取文件中存在的所有电子邮件。