eXcel的VBA使用正则expression式来validation电子邮件

所以我正在寻找的东西,因为我是新来的正则expression式,是否有可能取代我的“无效”返回语句与类似的声明注释掉在下一行,其中的字符和/或字符#expression失败了。

下面我们使用“msteede48 @ hotmail @ .com”作为myEmailvariables的示例input…

Option Explicit Function ValidEmail(myEmail As String) As String Dim regExp As Object Set regExp = CreateObject("VBScript.Regexp") If Len(myEmail) < 6 Then ValidEmail = "" Else With regExp .Global = True .IgnoreCase = True .Pattern = "^(?=[a-z0-9@.!#$%&'*+/=?^_`{|}~-]{6,254}$)(?=[a-z0-9.!#$%&'*+/=?^_`{|}~-]{1,64}@)[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:(?=[a-z0-9-]{1,63}\.)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?=[a-z0-9-]{1,63}$)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$" End With If regExp.Test(myEmail) = True Then ValidEmail = "Valid" Else ValidEmail = "Invalid" 'ValidEmail = "Error at character:" & CodeToDetermineWhatChar#TheRegexpFailedAtGoesHere & " with a(n):" & CodeToDetermineWhatCharTheRegexpFailedAtGoesHere & "." End If End If Set regExp = Nothing End Function 

输出如下所示:字符错误:18(a):@。