Regular Expression Checker in 10 minutes using Asp.net C# VB.Net

In most of the times we need to make or create a regular expression to find matching patterns from a given string or a file. Many languages support this feature like Javascript and also Asp.net C# & VB.Net. Regular expression makes our life easy. Few days ago i have done a dataminig project where i found that how much necessary the regular expression is. I understand its power and capability. The developer who wants to learn regular expression then first gather some knowledge on regular expression and then use this easy and simple tool to test the pattern matches.It will definitely increases your confidence as well as skills on Regular Expression.

Regular Expression Checker UI:
Make Regular Expression Checker in 10 minutes using Asp.net CSharp VB.Net

HTML Markup:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<table border="0">
        <tr>
            <td>Regular Expression: </td><td><asp:TextBox ID="txtExp" runat="server" Width="200px"></asp:TextBox></td>
        </tr>
        <tr>
            <td>Contents: </td><td><asp:TextBox ID="txtContent" runat="server" Columns="40" TextMode="MultiLine" Rows="5"></asp:TextBox></td>
        </tr>
        <tr>
            <td>Result: </td><td><asp:TextBox ID="txtResult" runat="server" Width="200px"></asp:TextBox></td>
        </tr>
        <tr>
            <td></td><td><asp:Button ID="cmdExecute" runat="server" Text="Execute" OnClick="cmdExecute_Click" /></td>
        </tr>
    </table>
</asp:Content>

Now under Execute button click event write the following server code:
C# Code:

    protected void cmdExecute_Click(object sender, EventArgs e)
    {
        string content = txtContent.Text;
        string pattern = txtExp.Text;
        MatchCollection mc = Regex.Matches(content, pattern);
        string sWord = "";
        if (mc.Count > 0)
        {
            for (int i = 0; i < mc.Count; i++)
            {
                if (sWord.Length == 0)
                    sWord = mc[i].Value;
                else
                    sWord = sWord + "," + mc[i].Value;
            }
        }
        txtResult.Text = sWord;
    }

Note: Don’t forget to add “System.Text.RegularExpressions” namespace.

VB.Net Code:

    Protected Sub cmdExecute_Click(sender As Object, e As System.EventArgs)
        Dim content As String = txtContent.Text
        Dim pattern As String = txtExp.Text
        Dim mc As MatchCollection = Regex.Matches(content, pattern)
        Dim sWord As String = ""
        If mc.Count > 0 Then
            For i As Integer = 0 To mc.Count - 1
                If sWord.Length = 0 Then
                    sWord = mc(i).Value
                Else
                    sWord = (sWord & ",") + mc(i).Value
                End If
            Next
        End If
        txtResult.Text = sWord

    End Sub

Note: Don’t forget to add “System.Text.RegularExpressions” namespace.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*