Tuesday, May 17, 2011

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




Please visit my new Web Site WWW.Codedisplay.com



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#. Regular expresion 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. So the regular expression example in back & forth.

The output looks like:
Regular Expression Checker

Design the UI in the following way:
<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>
Now under Execute button click event write the following server 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;
    }
Hope now you can test regular expressions in many different ways.

0 comments:

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash