- [DEVEXPRESS] Hỗ trợ tìm kiếm highlight không dấu và không khoảng cách trên Gridview Filter
- [C#] Chia sẻ source code phần mềm Image Downloader tải hàng loạt hình ảnh từ danh sách link url
- [C#] Chụp hình và quay video từ camera trên winform
- [C#] Chia sẽ full source code tách file Pdf thành nhiều file với các tùy chọn
- Giới thiệu về Stock Tracker Widget - Công cụ theo dõi cổ phiếu và cảnh báo giá tăng giảm bằng C# và WPF
- [VB.NET] Chia sẻ công cụ nhập số tiền tự động định dạng tiền tệ Việt Nam
- [VB.NET] Hướng dẫn fill dữ liệu từ winform vào Microsoft word
- [VB.NET] Hướng dẫn chọn nhiều dòng trên Datagridview
- Hướng Dẫn Đăng Nhập Nhiều Tài Khoản Zalo Trên Máy Tính Cực Kỳ Đơn Giản
- [C#] Chia sẻ source code phần mềm đếm số trang tập tin file PDF
- [C#] Cách Sử Dụng DeviceId trong C# Để Tạo Khóa Cho Ứng Dụng
- [SQLSERVER] Loại bỏ Restricted User trên database MSSQL
- [C#] Hướng dẫn tạo mã QRcode Style trên winform
- [C#] Hướng dẫn sử dụng temp mail service api trên winform
- [C#] Hướng dẫn tạo mã thanh toán VietQR Pay không sử dụng API trên winform
- [C#] Hướng Dẫn Tạo Windows Service Đơn Giản Bằng Topshelf
- [C#] Chia sẻ source code đọc dữ liệu từ Google Sheet trên winform
- [C#] Chia sẻ source code tạo mã QR MOMO đa năng Winform
- [C#] Chia sẻ source code phần mềm lên lịch tự động chạy ứng dụng Scheduler Task Winform
- [C#] Hướng dẫn download file từ Minio Server Winform
[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!

![[C#] Tìm và lấy tất cả email từ Text File](https://laptrinhvb.net/uploads/users/9a8cb514e4428e85fb4ca07588e9103f.png)

![[C#] Hướng dẫn sử dụng Data Binding Two Way giữa hai Form](https://laptrinhvb.net/uploads/source/csharp/databinding_thumb.png)
![[C#] Hướng dẫn viết chức năng tắt màn hình Monitor](https://laptrinhvb.net/uploads/source/image_baiviet/46bea25e78a39510978147e1045a7071.jpg)
![[C#] Hướng dẫn soạn thảo các biểu thức toán học vào lưu vào dữ liệu Sqlserver](https://laptrinhvb.net/uploads/source/image_baiviet/d4429d1083839a831926d54c1658cc32.png)
![[C#] Hướng dẫn thiết kế Label Vertical Align Text trong Winform](https://laptrinhvb.net/uploads/source/csharp/vertical_align_text_thumb.png)
![[C#] Hướng dẫn mã hóa và giải mã sử dụng thuật toán ROT13](https://laptrinhvb.net/uploads/source/image_baiviet/28b489d3bdb595e1799150aa858b847c.jpg)

![[C#] Hướng dẫn viết hiệu ứng chuyển động của label trong lập trình winform](https://laptrinhvb.net/uploads/source/csharp/animation_label_csharp.gif)

![[C#] Hướng dẫn sử dụng Row Filter trên DataView của DataTable](https://laptrinhvb.net/uploads/source/csharp/dataview_filter_csharp_thumb.jpg)
![[C#] Hướng dẫn xóa cache trình duyệt WebBrowser trong Csharp](https://laptrinhvb.net/uploads/source/image_baiviet/1567e1065489af572f9410510ca6a0cf.jpg)
![[C#] Viết ứng dụng get dữ liệu google suggest trong lap trinh csharp](https://laptrinhvb.net/uploads/source/image_baiviet/2ef41a883961b74f7e735053f1d41dad.png)


![[C#] Hướng dẫn viết ứng dụng chụp màn hình và tách chữ ra khỏi hình ảnh Winform](https://laptrinhvb.net/uploads/source/csharp/ocr_csharp_thumb.png)


![[C#] Hướng dẫn thêm, lưu, xóa, sửa, tìm kiếm trên SQLSERVER CE (SQL COMPACT)](https://laptrinhvb.net/uploads/source/image_baiviet/9695e729194410d52eb4a1e8234e9e0c.jpg)
![[C#] Hướng dẫn tách chữ tiếng việt ra khỏi hình ảnh (TESSERACT OCR )](https://laptrinhvb.net/uploads/source/csharp/ocr_vietnamese_thumb.png)
![[C#] Chia sẽ Class Download file chia thành nhiều phần nhỏ để tải](https://laptrinhvb.net/uploads/source/csharp/split_download_thumb.jpg)

![[POWERSHELL] Script sao lưu backup và nén database sqlserver](https://laptrinhvb.net/uploads/source/vbnet/backup_temp.jpg)
![[C#] Làm việc với NULL sử dụng toán tử ?? và ? trong lập trình winform](https://laptrinhvb.net/uploads/source/csharp/null_csharp_thumb.png)
![[C#] Giới thiệu thư viện Autofac Dependency Injection](https://laptrinhvb.net/uploads/source/vbnet/autoface_csharp.jpg)
![[C#] Hướng dẫn kiểm tra tình trạng dung lượng pin laptop](https://laptrinhvb.net/uploads/source/new_image_baiviet/pin_laptop.png)
![[C#] Hướng dẫn sử dụng Clipboard để copy, cut, paste và clear trong Winform](https://laptrinhvb.net/uploads/source/csharp/clip_board_thumb.png)
