有问题的正则表达式是
TldCheck = new Regex(@"(live|lycos|mail|yahoo|gmail|msn|netzero|peoplepc|sbcglobal|verizon|hotmail|earthlink|cox|bellsouth|comcast|barracuda|att|aol)\.?(com|net|org)@", RegexOptions.IgnoreCase | RegexOptions.Compiled);
当它针对列表运行时,会导致一些严重的滞后。有没有办法重写这个?其目的是清除电子邮件列表,例如:
adsfyahoocom@aol.com
adsfyahoocom@comcast.net
adfadyahoocom@gmail.com
最佳答案
加快速度的一种方法是执行以下操作。首先将所有域存储在 HashSet<string>
中。这是一个非常有效的查找结构,可以快速测试匹配
HashSet<string> hashSet = new HashSet<string>();
hashSet.Add("live");
hashSet.Add("lycos");
...
接下来,您需要拆分每个电子邮件地址,以便可以取出域名。最简单的方法是执行以下操作
string GetDomain(string email) {
var index = email.IndexOf('@');
var end = email.IndexOf('.', index + 1);
return email.Substring(index, end - index);
}
然后你就可以这样测试
if (hashSet.Contains(GetDomain("foo@bar.com")) {
...
}
关于c# - 当对我的代码进行基准测试时,这个正则表达式将循环的运行时间乘以 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20986886/