Tuesday, 19 July 2011

How to use Regex using ASP.NET 2.0 and C#.NET


This tutorial will show you how to Repeater pagesetting using ASP.NET 2.0 and C#

This tutorial will show you how to Repeater pagesetting using ASP.NET 2.0 and C#.NET .First, you  need to import the  System.Text.RegularExpressions namespace..

using System.Text.RegularExpressions;

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

We use the Button1_Click to do work.

The Regex class contains several static methods that allow you to use a regular expression without explicitly creating a Regex object. Using a static method is equivalent to constructing a Regex object, using it once and then destroying it. The Regex class is immutable (read-only) and is inherently thread safe. Regex objects can be created on any thread and shared between threads. The Methods of the Replace is Overloaded. Within a specified input string, replaces strings that match a regular expression pattern with a specified replacement string.

public partial class RegexCsharp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
Label2.Text = InputText(TextBox1.Text, TextBox1.Text.Length);
}

private string InputText(string text, int maxLength)
{
text = text.Trim();
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}
if (text.Length > maxLength)
{
text = text.Substring(0, maxLength);
}
text = Regex.Replace(text, "[\\s]{2,}", " ");
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n");
text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+"," ");
text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty);
text = text.Replace("'", "''");
return text;
}
}

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

The front end Default.aspx page looks something like this:

<asp:Label ID="Label1" runat="server" Text="Please Input:" Width="91px"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" Width="61px" />
<br />
<br />
<asp:Label ID="Label2" runat="server" Width="254px"></asp:Label></fieldset>
<br />
<br />

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

The flow for the code behind page is as follows.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

public partial class RegexCsharp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
Label2.Text = InputText(TextBox1.Text, TextBox1.Text.Length);
}

private string InputText(string text, int maxLength)
{
text = text.Trim();
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}
if (text.Length > maxLength)
{
text = text.Substring(0, maxLength);
}
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n");
text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+"," ");
text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty);
text = text.Replace("'", "''");
return text;
}
}

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment