Regular Expression Usage in .Net Environment

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

 

Regular Expression is a big topic and cannot be covered in a single article. I tried to explain it in brief.

 

In dot 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"))

{

    MessageBox.Show("match");

}

else

{

    MessageBox.Show("no match");

}

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)

 


Similar Articles