|
-
Feb 27th, 2009, 08:05 PM
#1
RegEx Email
When using .NET the pattern ....
\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
an encapsulated space will show as invalid but in VB6 it passes as valid but only the part after the space.
So..
dfgdfg [email protected]
is invalid in .net but in VB6 is returns [email protected] as the valid match.
I'm using VBScript_RegExp_55 in VB6.
So the question is if the procedure calling it is incorrect or is there some limitation using 5.5?
Modified procedure from VBF search
Code:
Public Function ValidateRegEx(Optional ByVal Target As Variant, Optional Pattern As String = "", Optional ByVal Item As Long = 0, _
Optional CaseSensitive As Boolean = False, Optional FailOnError As Boolean = True, Optional Persist As Boolean = False) As Boolean
'New email pattern: "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
Const SUB_NAME = ".ValidateRegEx"
Static oRE As Object 'VBScript_RegExp_55.RegExp
'Static declaration means we don't have to create and compile the RegExp
'object every single time the function is called.
Dim oMatches As Object 'VBScript_RegExp_55.MatchCollection
On Error GoTo errHandler
ValidateRegEx = False 'Default return value
'if FailOnError is false, returns Null on error
If IsMissing(Target) Then
'This is the signal to dispose of oRE
Set oRE = Nothing
Exit Function
End If
'Create the RegExp object if necessary
If oRE Is Nothing Then
Set oRE = CreateObject("VBScript.Regexp")
End If
With oRE
'Check whether the current arguments (other than Target)
'are different from those stored in oRE, and update them
'(thereby recompiling the regex) only if necessary.
If CaseSensitive = .IgnoreCase Then
.IgnoreCase = Not .IgnoreCase
End If
.Global = True
.MultiLine = True
If Pattern <> .Pattern Then
.Pattern = Pattern
End If
'Finally, execute the match
If IsNull(Target) Then
ValidateRegEx = False
Else
Set oMatches = oRE.Execute(Target)
If oMatches.Count > 0 Then
ValidateRegEx = True
Else
ValidateRegEx = False
End If
End If
End With
'Tidy up and exit
If Not Persist Then Set oRE = Nothing
Exit Function
errHandler:
Set oMatches = Nothing
Set oRE = Nothing
End Function
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 27th, 2009, 09:05 PM
#2
Re: RegEx Email
Rob, do you want to tell that "dfgdfg [email protected]" is invalid?
Code:
Private Sub Command1_Click()
Dim myReg As RegExp
Set myReg = New RegExp
myReg.IgnoreCase = True
myReg.Pattern = "^[\w-\.]+@\w+\.\w+$"
MsgBox myReg.Test("dfgdfg [email protected]")
MsgBox myReg.Test("[email protected]")
End Sub
-
Feb 27th, 2009, 09:22 PM
#3
Re: RegEx Email
Yes, well the pattern should work as it works in .net but not vb6
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 27th, 2009, 10:31 PM
#4
Re: RegEx Email
Using the pattern I have posted I get the same result with VB6.0 and C#. The pattern "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" returned true for all my test.
Code:
Regex r = new Regex(@"^[\w-\.]+@\w+\.\w+$");
MessageBox.Show(r.IsMatch("[email protected]").ToString()); //TRUE
MessageBox.Show(r.IsMatch("test [email protected]").ToString()); //FALSE
r = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
MessageBox.Show(r.IsMatch("[email protected]").ToString()); //TRUE
MessageBox.Show(r.IsMatch("test [email protected]").ToString()); //TRUE
Last edited by dee-u; Feb 27th, 2009 at 10:34 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|