Results 1 to 4 of 4

Thread: RegEx Email

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Arrow 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width