在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中讀取檔案中所有電子郵件。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP