While doing some coding the other day I needed an expression to validate email addresses. The trick was that it had to be flexible enough to handle emails in the format of username@company.com and firstname.lastname@company.com.

 

Imports System.Text.RegularExpressions

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim strEmail As String = "thom.robbins@microsoft.com"

        Dim regexp As New Regex("^([\w-]+\.)*?[\w-]+@[\w-]+\.([\w-]+\.)*?[\w]+$")

 

        If regexp.IsMatch(strEmail) Then

            MsgBox("Valid Email")

        Else

            MsgBox("Invalid Email")

        End If

End Sub