I'm writing this small program to extract any number of email address from a text file. I am getting two errors, "Use of unassigned local variable." and I'm not sure why.
static void Main(string[] args)
{
string InputPath = @"C:\Temp\excel.txt";
string OutputPath = @"C:\Temp\emails.txt";
string EmailRegex = @"^(?:[a-zA-Z0-9_'^&/+-])+(?:\.(?:[a-zA-Z0-9_'^&/+-])+)*@(?:(?:\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)|(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z]){2,}\.?)$";
string Text = String.Empty;
StringBuilder Output;
List<string> Emails;
using (TextReader tr = new StreamReader(InputPath))
{
Text = tr.ReadToEnd();
}
MatchCollection Matches = Regex.Matches(Text,EmailRegex);
foreach (Match m in Matches)
{
Emails.Add(m.ToString().Trim()); // one error is here
}
foreach (String s in Emails)
{
Output.Append(s + ","); // the other error is here
}
using (TextWriter tw = new StreamWriter(OutputPath))
{
tw.Write(Output.ToString());
}
}
Sorry for the formatting ... I'm kind of pressed for time!
edit: WOW. I'm an idiot - must be because I'm pressed for time!!!!