.net(正则表达式)检查EMAIL

前端之家收集整理的这篇文章主要介绍了.net(正则表达式)检查EMAIL前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
using System;
using System.Globalization;
using System.Text.RegularExpressions;

publicclass RegexUtilities
{
   bool invalid = false;

   publicbool IsValidEmail(string strIn)
   {
       invalid = false;
       if (String.IsNullOrEmpty(strIn))
          returnfalse;

       // Use IdnMapping class to convert Unicode domain names.
       try {
          strIn = Regex.Replace(strIn,@"(@)(.+)$",this.DomainMapper,RegexOptions.None,TimeSpan.FromMilliseconds(200));
       }
       catch (RegexMatchTimeoutException) {
         returnfalse;
       }

       if (invalid) 
          returnfalse;

       // Return true if strIn is in valid e-mail format.
       try {
          return Regex.IsMatch(strIn,21)">@"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" + 
                @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$",RegexOptions.IgnoreCase,TimeSpan.FromMilliseconds(250));
       }  
       catch (RegexMatchTimeoutException) {
          returnfalse;
       }
   }

   privatestring DomainMapper(Match match)
   {
      // IdnMapping class with default property values.
      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;
   }
}
 
publicclass Application
{
   publicstaticvoid Main()
   {
      RegexUtilities util = new RegexUtilities();
      string[] emailAddresses = { "david.jones@proseware.com",21)">"d.j@server1.proseware.com",21)">"jones@ms1.proseware.com",21)">"j.@server1.proseware.com",21)">"j@proseware.com9",21)">"js#internal@proseware.com",21)">"j_9@[129.126.118.1]",21)">"j..s@proseware.com",21)">"js*@proseware.com",21)">"js@proseware..com",21)">"js@proseware.com9",21)">"j.s@server1.proseware.com" };

      foreach (var emailAddress in emailAddresses) {
         if (util.IsValidEmail(emailAddress))
            Console.WriteLine("Valid: {0}",emailAddress);
         else
            Console.WriteLine("Invalid: {0}",emailAddress);
      }                                            
   }
}
原文链接:https://www.f2er.com/regex/362717.html

猜你在找的正则表达式相关文章