Onega

a lot of VC++ posts, a few C# posts, and some miscellaneous stuff

Thursday, May 04, 2006

C# 2005 code to extract host name from email address via regular expression

using System;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
namespace email2domain
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Extract domain from string like "Old Navy OfferConfirm [onav@splendiddealalerts.com]"
//By Onega, in VC# 2005
//reference:
//http://support.microsoft.com/kb/308252
//http://www.windowsdevcenter.com/pub/a/oreilly/windows/news/csharp_0101.html
//C# Regular Expressions by Brad Merrill
//Oreilly.CSharp.Cookbook
string output_file_name = @"D:\Onega\email\email_filters_out.txt";
if (File.Exists(output_file_name))
File.Delete(output_file_name);
StreamWriter output_file = new StreamWriter(output_file_name);
StreamReader input_file = new StreamReader(@"D:\Onega\email\email_filters.txt");
String regex_pattern = "(?[^@]+)@(?.+)\\]";
Regex emailregex = new Regex(regex_pattern);
while(true)
{
string s = input_file.ReadLine();
if ( null == s || s.Length < 1)
break;
Match m = emailregex.Match(s);
if(m.Success)
{
output_file.WriteLine(m.Groups["host"].Value);
}
}
output_file.Close();
input_file.Close();
}
}
}

0 Comments:

Post a Comment

<< Home