C# program to extract all email addresses from a text file

C# program to extract email addresses from a file:

In this post, we will learn how to extract email addresses from a file. The path of the file is given and the program will read and extract all email addresses from that file.

With this program, you will learn how to read the content of a file and how to use regular expressions to extract the email addresses from the file.

C# program to extract all emails from a file:

Let’s write down the C# program. This program is reading the content from a text file in the same folder. Before you run this program, create one file input.txt in the same folder and add the text to it. Once you run this program, it will find all email addresses in that file and print these on the console.

using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;

namespace Example
{
    class Program
    {

        static void Main(string[] args)
        {
            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})";
            
            string inputFile = File.ReadAllText(@"input.txt");

            Regex pattern = new Regex(PATTERN);
            
            MatchCollection matches = pattern.Matches(inputFile);
    
            Console.WriteLine("Email Addresses found:");
            foreach (Match match in matches)
            {
                Console.WriteLine(match.Value);
            }
        }
    }
}
  • The PATTERN is the regex pattern that matches all email addresses.
  • It uses File.ReadAllText to read the content of the file. Note that if the file is not found, it will throw an exception. It reads the content of the file and assigns it to the string variable inputFile.
  • Once the file content is read, it creates a Regex object with the pattern.
  • It calls the Matches() function on the Regex object to find all matches. The string variable that is holding the content of the file is passed as its parameter.
  • It returns one MatchCollection object. We can use a foreach loop to iterate over the matches. This program is printing all matching values.

input.txt file content:

For this example, we are using the below content in the input.txt file.

hello##one@gmail.com##%%^
#two@**&&two@gmail.com##***hello###three@rediffmail.com

You can also try any other content that you prefer.

Once the above program is run, it will print the below email addresses:

Email Addresses found:
one@gmail.com
two@gmail.com
three@rediffmail.com

C# program to extract all emails and write down into a file:

Let’s change the above program to extract all emails and write them to a separate file. We have to make the following changes to the program:

using System.Text.RegularExpressions;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            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})";
            
            string inputFile = File.ReadAllText(@"input.txt");

            Regex pattern = new Regex(PATTERN);
            
            MatchCollection matches = pattern.Matches(inputFile);

            using (StreamWriter file = new StreamWriter(@"output.txt")) {
                    foreach (Match match in matches) {
                        file.WriteLine(match.Value);
                    }
            }

            Console.WriteLine("Email Addresses are written to the file.");
        }
    }
}

Here,

  • We are using a StreamWriter object to open a new file output.txt in the same folder. It will create that file if the file is not there. If the file has content, it will replace the content.
  • The foreach loop is iterating over the matches similar to the previous example. It writes the lines to the file by using the WriteLine function.

If you run this program, it will write the emails to the output.txt file.

You might also like: