This Class uses String arrays to store results
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Coderbuddy
{
public class ExtractEmails
{
private string s;
public ExtractEmails(string Text2Scrape)
{
this.s = Text2Scrape;
}
public string[] Extract_Emails()
{
string[] Email_List = new string[0];
Regex r = new Regex(@"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}", RegexOptions.IgnoreCase);
Match m;
//Searching for the text that matches the above regular expression(which only matches email addresses)
for (m = r.Match(s); m.Success; m = m.NextMatch())
{
//This section here demonstartes Dynamic arrays
if (m.Value.Length > 0)
{
//Resize the array Email_List by incrementing it by 1, to save the next result
Array.Resize(ref Email_List, Email_List.Length + 1);
Email_List[Email_List.Length - 1] = m.Value;
}
}
return Email_List;
}
}
}
The following code snippet is the same as the above one, the only difference is that this method saves the Extracted Email Id’s to a string list instead of an array, I think the following code is more effective and easy to code
This class uses List to store results, and these list are converted into arrays just before they are returned as arrays
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Coderbuddy
{
public class ExtractEmailLists
{
private string s;
public ExtractEmailLists(string Text2Scrape)
{
this.s = Text2Scrape;
}
public string[] Extract_Emails()
{
Regex r = new Regex(@"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}", RegexOptions.IgnoreCase);
Match m;
List results = new List();
for (m = r.Match(s); m.Success; m = m.NextMatch())
{
if (!(results.Contains(m.Value)))
results.Add(m.Value);
}
return results.ToArray();
}
}
}