Skip to content

Instantly share code, notes, and snippets.

@bobixaka
Created August 28, 2013 21:06
Show Gist options
  • Save bobixaka/6371291 to your computer and use it in GitHub Desktop.
Save bobixaka/6371291 to your computer and use it in GitHub Desktop.
How to extract e-mails from a text file with Regex (Regular expression)
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace Task_18_E_MailExtract
{
class MailExtractor
{
static void Main()
{
StreamReader Input = new StreamReader(@"..\..\InputFile.txt");
string InputContent = "";
StringBuilder Output = new StringBuilder();
string Pattern = @"\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b";
Regex mail = new Regex(Pattern,RegexOptions.IgnoreCase);
using (Input)
{
InputContent = Input.ReadToEnd();
}
MatchCollection MC = mail.Matches(InputContent);
foreach (var match in MC)
{
Output.AppendLine(match.ToString());
}
Console.WriteLine(Output);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment