- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
- [C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
- [C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF
[C#] Tìm và lấy tất cả email từ Text File
Xin chào các bạn, bài viết hôm nay mình chia sẻ các bạn đoạn code giùm để tìm và lấy tất cả các email từ trong 1 file text.
[C#] Extract Email Addresses from Text File
Ví dụ: các bạn có file text input.txt
như sau
This text file contains eddiejackson1@test.com AA aa BB bb.
This text file contains eddiejackson2@test.com CC cc DD dd.
This text file contains eddiejackson3@test.com EE ee FF ff.
This text file contains eddiejackson4@test.com 1234567890.
This text file contains eddiejackson5@test.com 1234567890.
This text file contains eddiejackson6@test.com 1234567890.
và kết quả chúng ta mong muốn là lấy danh sách email từ file input.txt
xuất ra file text output.txt
như sau:
eddiejackson1@test.com
eddiejackson2@test.com
eddiejackson3@test.com
eddiejackson4@test.com
eddiejackson5@test.com
eddiejackson6@test.com
Source code extract email from text file C#:
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
class ExtractEmailAddresses
{
public static void Main()
{
// read data
string input = File.ReadAllText(@"C:\csharp\return_email\input.txt");
//Console.Write(input);
// email address pattern
const string Pattern =
@"(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
+ @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
+ @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+ @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})";
// set up regex instance with options
Regex emailPattern = new Regex(Pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
// perform match
MatchCollection emailMatches = emailPattern.Matches(input);
// set up our string builder
StringBuilder sb = new StringBuilder();
// build the list
Console.WriteLine("---EXTRACTED EMAIL ADDRESSES---");
foreach (Match emailMatch in emailMatches)
{
// add address to builder
sb.AppendLine(emailMatch.Value);
// display to console
Console.WriteLine("\n {0}", emailMatch.Value);
}
// write to file
File.WriteAllText(@"C:\csharp\return_email\output.txt", sb.ToString());
// exit
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
}
Thanks for watching!