Right now I am using a regex to extract emails from a text document, and filter the duplicates out of this using the Distinct function from linq. After which I run the output file through a clean up of duplicates as well, as the program can be run several times over several different files.
Now my question, would there be a more optimal way of checking the output file for duplicates? Because as it is right now, the bigger the file, the longer it will take.
And I have the feeling there should be a less intense way to check this.
// Extractor
public void Mail(string file)
{
string strRegex = @"[A-Za-z0-9_\-\+]+@[A-Za-z0-9\-]+\.([A-Za-z]{2,3})(?:\.[a-z]{2})?";
var myRegex = new Regex(strRegex, RegexOptions.None);
var matches = new List<string>();
foreach (Match myMatch in myRegex.Matches(file))
{
if (myMatch.Success)
{
matches.Add(myMatch.Value);
}
}
var cleanMatch = matches.Distinct().ToList();
for (var i = 0; i < cleanMatch.Count; i++)
{
Log.Mail(cleanMatch[i]);
}
CleanDuplicates();
}
// Duplicate Cleaner
private void CleanDuplicates()
{
var lines = new List<string>(System.IO.File.ReadAllLines(System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory) + "\\EmailList.txt"));
lines = lines.Distinct().ToList();
string target = System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory) + "\\EmailList.txt";
using (System.IO.StreamWriter newTask = new System.IO.StreamWriter(target, false))
{
for (var i = 0; i < lines.Count; i++)
{
newTask.WriteLine(lines[i]);
}
}
}