

Navigator : Home > Tutorials > Advanced Technologies Tutorials > ...
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; |
We use the Button1_Click to do work.
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
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; } } |
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 are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
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!
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!