使用 c# 从文本中提取所有电子邮件地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2333835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
extract all email address from a text using c#
提问by Thunder
Is there a way to extract all email addresses from a plain text using C# .
有没有办法使用 C# 从纯文本中提取所有电子邮件地址。
For example
例如
my email address is [email protected] and his email is [email protected]
我的电子邮件地址是 [email protected],他的电子邮件是 [email protected]
should return
应该回来
[email protected], [email protected]
[email protected]、[email protected]
I have tried the 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;
}
采纳答案by Thunder
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());
}
}
回答by David Morton
Just remove the "^" from the beginning and the "$" from the end of your filter string.
只需从过滤器字符串的开头删除“^”和“$”。
回答by Scott Vercuski
回答by Oded
If you don't want it to match perfect email addresses, don't use a regular expression that matches perfect email addresses.
如果您不希望它匹配完美的电子邮件地址,请不要使用匹配完美电子邮件地址的正则表达式。
The regular expression you are using will match on the start of the line (^) and the end of the line ($), so if you remove those it will not filter with them.
您使用的正则表达式将匹配行首 (^) 和行尾 ($),因此如果您删除它们,则不会使用它们进行过滤。
回答by Meysam Javadi
check this snippet
检查这个片段
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
class MailExtracter
{
public static void ExtractEmails(string inFilePath, string outFilePath)
{
string data = File.ReadAllText(inFilePath); //read File
//instantiate with this pattern
Regex emailRegex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*",
RegexOptions.IgnoreCase);
//find items that matches with our pattern
MatchCollection emailMatches = emailRegex.Matches(data);
StringBuilder sb = new StringBuilder();
foreach (Match emailMatch in emailMatches)
{
sb.AppendLine(emailMatch.Value);
}
//store to file
File.WriteAllText(outFilePath, sb.ToString());
}
}