从文本使用C#提取所有的电子邮件地址 [英] extract all email address from a text using c#
本文介绍了从文本使用C#提取所有的电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法提取纯文本的所有电子邮件地址,使用C#。
比如我的电子邮件地址是mrrame@gmail.com和他的电子邮件是mrgar@yahoo.com应该返回 mrrame @ gmail.com,mrgar @ yahoo.com
我曾尝试以下,但它只有匹配完美的电子邮件。
公共常量字符串MatchEmailPattern =
@^(([\ w - ] + \)+ [\ w - ] + |([A-ZA-Z] {1} | [\ w - ] {2}))@
+ @((([0-1] [0-9] {1,2} |?25 [0-5] | 2 [0-4] [0-9])\([0-1] ?[0-9] {1,2} | 25 [0-5] | 2 [0-4] [0-9])\。
+ @。?([0-1] [0-9] {1,2} |?25 [0-5] | 2 [0-4] [0-9])\([0-1] [ 0-9] {1,2} | 25 [0-5] | 2 [0-4] [0-9])){1} |
+ @([A-ZA-Z] + [\ w - ] + \。)+ [A-ZA-Z] {2,4})$;
公共静态布尔IsEmail(字符串email)
{
如果(电子邮件!= NULL)返回Regex.IsMatch(电子邮件,MatchEmailPattern);
否则返回false;
}
解决方案
以下工作
公共静态无效的EMAS(文本字符串)
{
常量字符串MatchEmailPattern =
@(([\ w - ] + \)+ [\ w - ] + |([A-ZA-Z] {1} | [\ w - ] {2}))@
+ @((([0-1] [0-9] {1,2} |?25 [0-5] | 2 [0-4] [0-9])\([0-1] ?[0-9] {1,2} | 25 [0-5] | 2 [0-4] [0-9])\。
+ @。?([0-1] [0-9] {1,2} |?25 [0-5] | 2 [0-4] [0-9])\([0-1] [ 0-9] {1,2} | 25 [0-5] | 2 [0-4] [0-9])){1} |
+ @([A-ZA-Z] + [\ w - ] + \。)+ [A-ZA-Z] {2,4});
正则表达式RX =新的正则表达式(MatchEmailPattern,RegexOptions.Compiled | RegexOptions.IgnoreCase);
//查找匹配。
MatchCollection匹配= rx.Matches(文本);
//报告发现匹配的数量。
INT noOfMatches = matches.Count;
//对每场比赛的报告。
的foreach(在比赛中赛赛)
{
Console.WriteLine(match.Value.ToString());
}
}
is there a way to extract all email address from a plain text,Using C# .
eg my email address is mrrame@gmail.com and his email is mrgar@yahoo.com should return mrrame@gmail.com,mrgar@yahoo.com
I have tried following but it matches perfect emails only.
public const string MatchEmailPattern =
@"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
+ @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
+ @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+ @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";
public static bool IsEmail(string email)
{
if (email != null) return Regex.IsMatch(email, MatchEmailPattern);
else return false;
}
解决方案
Following works
public static void emas(string text)
{
const string MatchEmailPattern =
@"(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
+ @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
+ @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+ @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})";
Regex rx = new Regex(MatchEmailPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
// Find matches.
MatchCollection matches = rx.Matches(text);
// Report the number of matches found.
int noOfMatches = matches.Count;
// Report on each match.
foreach (Match match in matches)
{
Console.WriteLine(match.Value.ToString());
}
}
这篇关于从文本使用C#提取所有的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文