【问题标题】:extract all email address from a text using c#使用 c# 从文本中提取所有电子邮件地址
【发布时间】:2011-01-20 23:42:46
【问题描述】:

有没有办法使用 C# 从纯文本中提取所有电子邮件地址。

例如

我的电子邮件地址是 mrrame@gmail.com,他的电子邮件是 mrgar@yahoo.com

应该返回

mrrame@gmail.com、mrgar@yahoo.com

我尝试了以下方法,但它只匹配完美的电子邮件。

 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;
        }

【问题讨论】:

  • 删除正则表达式开头的 ^ 和末尾的 $。此活动除了发送垃圾邮件还有其他目的吗?
  • @Lazarus 不是用于发送垃圾邮件,而是用于网络爬取地址以便建立联系人数据库!删除 ^ 和 $ 有效,但必须添加一些调整以从中提取电子邮件。我已在此发布答案。
  • 试试这个stackoverflow.com/a/26274085/1604425 比复杂的正则表达式好得多

标签: c# .net regex email


【解决方案1】:

只需删除过滤器字符串开头的“^”和末尾的“$”即可。

【讨论】:

  • 删除 ^ 和 $ 有效,但必须添加一些调整以从中提取电子邮件。我已在此发布答案
【解决方案2】:

【讨论】:

  • “官方标准:RFC 2822”部分特别重要,如果您真的想了解尝试查找有效电子邮件地址的过程。
  • 电子邮件的 RFC 有哪些:RFC 1035、RFC 5322 和 RFC 2822?
  • 谢谢你非常有用的文章,它深入研究了模式匹配;清楚地解释每个部分
【解决方案3】:

如果您不希望它匹配完美的电子邮件地址,请不要使用匹配完美电子邮件地址的正则表达式。

您使用的正则表达式将匹配行首 (^) 和行尾 ($),因此如果您删除它们,它将不会与它们一起过滤。

【讨论】:

    【解决方案4】:

    检查这个sn-p

    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());
        }
    }
    

    【讨论】:

      【解决方案5】:

      以下作品

      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());
           }
      }
      

      【讨论】:

      猜你喜欢
      • 2014-03-23
      • 1970-01-01
      • 2016-01-14
      • 1970-01-01
      • 2016-04-12
      • 2011-08-24
      • 1970-01-01
      • 2015-12-28
      相关资源
      最近更新 更多