Tuesday, 19 July 2011

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 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.

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.

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

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

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 />

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

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!

No comments:

Post a Comment