Gmail, Yahoo Mail, Hotmail,… rất nhiều loại email, làm thế nào để viết hàm lọc chúng ra thành một danh sách? Sử dụng một cái máy nghiền dữ liệu chăng @@
Sau đây sẽ là một cách khá hay mà mình đã sử dụng, đầu vào dữ liệu rất khỏe, vứt tất cả các string mà bạn có được, kết quả sẽ ra một danh sách email như ý 😉
Mấu chốt của hàm là các chuỗi sử dụng Regex (Regex là gì thì các bạn có thể đọc tại đây).
Bắt đầu thôi, trước tiên chúng ta cần các hàm trợ giúp gọi là Helper Methods. Mục đích chính là để kiểm tra dữ liệu đầu vào để tránh trường hợp bỏ các thứ linh tinh vào thì lọc không được.
Helper Methods
Hàm kiểm tra một chuỗi có đúng là email không? Kết quả trả về bool:
private bool IsValidEmail(string strIn)
{
invalid = false;
if (String.IsNullOrEmpty(strIn))
return false;
try
{
strIn = Regex.Replace(strIn, @"(@)(.+)$", this.DomainMapper,
RegexOptions.None, TimeSpan.FromMilliseconds(200));
}
catch (RegexMatchTimeoutException)
{
return false;
}
if (invalid)
return false;
// Return true if strIn is in valid e-mail format.
try
{
return Regex.IsMatch(strIn,
@"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
}
catch (RegexMatchTimeoutException)
{
return false;
}
}
Hàm nhận biết email theo domain (những email như [email protected] chẳng hạn). Kết quả trả về string:
private string DomainMapper(Match match)
{
IdnMapping idn = new IdnMapping();
string domainName = match.Groups[2].Value;
try
{
domainName = idn.GetAscii(domainName);
}
catch (ArgumentException)
{
invalid = true;
}
return match.Groups[1].Value + domainName;
}
Tiếp theo là các hàm lọc email.
Lọc tất cả các loại email (miễn là email thì lọc hết)
public static string FilterAll(string input)
{
string strReturn = "";
List<string> myCollection = new List<string>();
Regex validationExpression = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
MatchCollection matchCollection = validationExpression.Matches(input);
foreach (var matchedEmailAddress in matchCollection)
{
myCollection.Add(matchedEmailAddress.ToString());
}
myCollection.ForEach(delegate(String name)
{
if (!strReturn.Contains(name))
{
strReturn += name + "\n";
}
});
return strReturn;
}
Lọc tất cả các email Gmail
public static string FilterGmail(string input)
{
string strReturn = "";
List myCollection = new List();
Regex validationExpression = new Regex(@"\w+([-+.']\w+)*@(?:gmail).com");
MatchCollection matchCollection = validationExpression.Matches(input);
foreach (var matchedEmailAddress in matchCollection)
{
myCollection.Add(matchedEmailAddress.ToString());
}
myCollection.ForEach(delegate(String name)
{
if (!strReturn.Contains(name))
{
strReturn += name + "\n";
}
});
return strReturn;
}
Lọc tất cả các email Yahoo
public static string FilterYahooMail(string input)
{
string strReturn = "";
List myCollection = new List();
Regex validationExpression = new Regex(@"\w+([-+.']\w+)*@(?:yahoo).com");
MatchCollection matchCollection = validationExpression.Matches(input);
foreach (var matchedEmailAddress in matchCollection)
{
myCollection.Add(matchedEmailAddress.ToString());
}
myCollection.ForEach(delegate(String name)
{
if (!strReturn.Contains(name))
{
strReturn += name + "\n";
}
});
return strReturn;
}
Lọc tất cả các email Hotmail
public static string FilterHotMail(string input)
{
string strReturn = "";
List myCollection = new List();
Regex validationExpression = new Regex(@"\w+([-+.']\w+)*@(?:hotmail).com");
MatchCollection matchCollection = validationExpression.Matches(input);
foreach (var matchedEmailAddress in matchCollection)
{
myCollection.Add(matchedEmailAddress.ToString());
}
myCollection.ForEach(delegate(String name)
{
if (!strReturn.Contains(name))
{
strReturn += name + "\n";
}
});
return strReturn;
}
Lọc tất cả các email Outlook
public static string FilterOutlook(string input)
{
string strReturn = "";
List myCollection = new List();
Regex validationExpression = new Regex(@"\w+([-+.']\w+)*@(?:outlook).com");
MatchCollection matchCollection = validationExpression.Matches(input);
foreach (var matchedEmailAddress in matchCollection)
{
myCollection.Add(matchedEmailAddress.ToString());
}
myCollection.ForEach(delegate(String name)
{
if (!strReturn.Contains(name))
{
strReturn += name + "\n";
}
});
return strReturn;
}
Lọc tất cả các email theo domain
public static string FilterOther(string input, string query)
{
string strReturn = "";
string start = query.Trim().Substring(0, query.LastIndexOf("."));
string end = query.Trim().Substring(query.LastIndexOf("."), query.Length - query.LastIndexOf("."));
List myCollection = new List();
Regex validationExpression = new Regex(@"\w+([-+.']\w+)*@(?:" + start + ")" + end);
MatchCollection matchCollection = validationExpression.Matches(input);
foreach (var matchedEmailAddress in matchCollection)
{
myCollection.Add(matchedEmailAddress.ToString());
}
myCollection.ForEach(delegate(String name)
{
if (!strReturn.Contains(name))
{
strReturn += name + "\n";
}
});
return strReturn;
}
Để xem các hàm trên hoạt động như thế nào, các bạn có thể xem bài viết mình đã giới thiệu công cụ lọc email từ website, công cụ này được xây dựng từ các hàm trên. Ngoài ra còn có các hàm lọc số điện thoại và website, mình sẽ chia sẻ trong các bài viết tới.
Chúc các bạn thành công!


