ARTICLE

Regular Expression usage in .NET environment

Posted by vinod shalgar Articles | Visual Basic 2010 August 17, 2007
This article describes how to use regular expression in VB.NET.
 
Reader Level:

In this article I will discuss about regular expression (or RegEx in short) class in .Net.

 

Regular Expression is a big topic and can't be covered in a single article. I am trying to explain it in brief.

 

In .NET there is a strong support for regular expression class. A regular expression is a string that is used to describe match based on certain rule. Regular expression is a very powerful tool for pattern matching.

 

Regular Expression can be found in System.Text.RegularExpressions namespace.

 

Example for using regular expression:

If Regex.IsMatch("ASP.Net", "ASP") Then

          MessageBox.Show("match")

Else

          MessageBox.Show("no match")

End If

 

Above example ("where "ASP" is a pattern") search for the matching world "ASP" in the string "ASP.NET", if match found, shows message "match".

 

Similar way different pattern can be passed for searching a particular string.

 

Note: Regular Expression is a case sensitive; "a" is different from "A".


Example: 
 

String

Pattern

Result

Sales001

Sale.

True

Sal123

Sale.

False

Sale.xls

Sale.

True

Sale.xls

sale.

False 

Sale

.a.

True

Some of the list of patterns.

Pattern

Matches

*.

Searches any string

Sales$

Searches string which ends with "Sales"

Example : Sales, finale Sales, year 2007 Sales

^v[i,v]k$

vik, vvk (string starts with v and ends with k and contains i and v)

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# Corner (http://www.c-sharpcorner.com/).

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor