1

メールID検証用の次のコードがあります。私はそれを行うために正規表現を使用しています。ただし、次のメールIDは無効になっています。

test_@gmail.com

test_@gmail.comメールID( )を渡すために正規表現で何を変更する必要があるか

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

            // Use IdnMapping class to convert Unicode domain names. 
            try
            {

                strIn = Regex.Replace(strIn, @"(@)(.+)$", DomainMapper);

            }
            catch (Exception e)
            {
                Utility.writeToLogFile(String.Format("Error while validating the Email Address {0}. Error Code.e", strIn.ToString(), e));
                return false;
            }

            if (invalid)
                return false;

            // Return true if strIn is in valid e-mail format. 
            try
            {
                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);


            }
            catch (Exception e)
            {
                Utility.writeToLogFile(String.Format("Error while validating the Email Address {0}. Error Code.e", strIn.ToString(), e));
                return false;
            }
        }
4

1 に答える 1

1

あなたの正規表現は失敗しています

test_@gmail.com

なぜなら、その部分(?<=[0-9a-z])@は「@」の前に明示的に文字または数字を必要とするからです。

解決策として、コメントのEduard Dumitruのように、この回答も提案します。

于 2013-02-26T09:43:06.333 に答える