2012-03-14 9 views
1

を検証しません?正規表現は次のように私は、このユーティリティの機能を持っているドメイン名

更新:

public class RegexUtilities 
    { 
     bool invalid; 

     public bool IsValidEmail(string strIn) 
     { 
      invalid = false; 
      if (String.IsNullOrEmpty(strIn)) 
       return false; 

      // Use IdnMapping class to convert Unicode domain names. 
      strIn = Regex.Replace(strIn, @"(@)(.+)$", DomainMapper); 
      if (invalid) 
       return false; 

      // Return true if strIn is in valid e-mail format. 
      return Regex.IsMatch(strIn, 
           @"^(?("")(""[^""]+?""@)|(([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); 
     } 

     public bool IsValidDomainName(string strIn) 
     { 
      return Regex.IsMatch(strIn, @"^([a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+.*)$"); 
     } 

     private string 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; 
     } 
    } 
+5

ご不便をおかけしてください。 :) – eandersson

+0

どのように失敗ですか? – MoonKnight

+0

入力文字列とは何ですか? –

答えて

1

はこれを試してみてください:

を使用すると、プロトコルのHTTPを前に置く強制する場合://、https://でなど

^(http://|https://|ftp://)\w+\.\w+.*$ 

あなたの場合プロトコルhttp://、https://などのプレフィックスを強制したくない。

^(http://|https://|ftp://)?\w+\.\w+.*$ 
関連する問題