VB.Net Using Regex

How To Use Regular Expressions In Visual Basic .Net

Using Regular Expressions In Visual Basic .Net



In this VB.NET Tutorial we will see How To Use Regular Expression To Verfiy Email Address, Phone Number, Website URL On A Button Click Event In Visual Basic.Net Programming Language And Visual  Studio Editor.




Project Source Code:


Imports System.Text.RegularExpressions

Public Class Regular_Expression

    Private Sub ButtonVerify_Click(sender As Object, e As EventArgs) Handles ButtonVerify.Click


        ' mail@mail.com                          =>  ^([\w]+)@([\w]+)\.([\w]+)$
        ' http(s)://www.google.com               =>  ^((http|https)://www\.)([\w]+)\.([\w]+)$
        ' Phone Number Like : 0011 *** *** ***   =>  ^(0011)(([  ][0-9]{3}){3})$

        execReg("^([\w]+)@([\w]+)\.([\w]+)$", TextBoxEmail, LabelEmail)
        execReg("^(0011)(([  ][0-9]{3}){3})$", TextBoxPhone, LabelPhone)
        execReg("^((http|https)://www\.)([\w]+)\.([\w]+)$", TextBoxWebsite, LabelSite)

    End Sub


    Sub execReg(ByVal pattern As String, ByVal tbx As TextBox, ByVal lbl As Label)

        Dim rgx As New Regex(pattern)

        If rgx.IsMatch(tbx.Text) Then

            lbl.Text = "Valid"
            lbl.ForeColor = Color.Green

        Else

            lbl.Text = "InValid"
            lbl.ForeColor = Color.Red

        End If

    End Sub

End Class


OutPut:

Visula Basic .Net Regular Expressions




Share this

Related Posts

Previous
Next Post »