DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 How to use Regex using ASP.NET 2.0 and VB.NET

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

We use the Button1_Click to do work.

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.


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.

Partial Class RegexVB
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Label2.Text = InputText(TextBox1.Text, TextBox1.Text.Length)
End Sub

Private Function InputText(ByVal text As String, ByVal maxLength As Integer) As String
text = text.Trim()
If (String.IsNullOrEmpty(text)) Then
Return String.Empty
End If
If (text.Length > maxLength) Then
text = text.Substring(0, maxLength)
End If
text = Regex.Replace(text, "[\\s]{2,}", " ")
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "")
text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " ")
text = Regex.Replace(text, "<(.|\\n)*?>", "")
text = text.Replace("'", "''")
Return text
End Function

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub
End Class

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.

Imports System.Text.RegularExpressions

Partial Class RegexVB
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Label2.Text = InputText(TextBox1.Text, TextBox1.Text.Length)
End Sub

Private Function InputText(ByVal text As String, ByVal maxLength As Integer) As String
text = text.Trim()
If (String.IsNullOrEmpty(text)) Then
Return String.Empty
End If
If (text.Length > maxLength) Then
text = text.Substring(0, maxLength)
End If

text = Regex.Replace(text, "[\\s]{2,}", " ")
text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "")
text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " ")
text = Regex.Replace(text, "<(.|\\n)*?>", "")
text = text.Replace("'", "''")
Return text
End Function

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub
End Class




Looking for the C# 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.


Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!