Dot NET Regular Expression

Hi, in this post, I will demonstrate how to use a Regular Expression using the Dot NET Framework. If you are new to Regular Expressions, this post would not be of much use. This is intended for an audience who knows regular expressions and need to implement a regular expression in their Dot NET code.
There may be scenarios where you need to match a certain string with a Regular Expression. Using the Dot NET Framework, this becomes relatively easy.

First, you need to import the namespace by defining

using System.Text.RegularExpressions;

Then, you can create a Regex object by calling its constructor. This is an overloaded constructor. I will show only one use of it. (You can explore the other!!!)

Regex regularExpression = new Regex("");

This would create a Regex object. Once created, you can call several methods to get the job done.
I will show how the Replace method can be used to identify a character pattern and replace it with a character set of your own. (A good example is when you want to replace offensive words entered by a user with asterisks in a social networking site)

Regex regularExpression = new Regex("ruchira[a-z]*");
string filteredWords = regularExpression.Replace("ruchira", "*");

After performing the above, the result would be a single asterix. ie: "ruchira" would be replaced by "*"

I believe this is a good point to start with Regular Expressions, you can continue exploring its other functionalities.

Cheers !!!!!!!!

Comments

Popular posts from this blog

Encrypt and Decrypt Images Using Java

Build your own Network sniffer

ASP Response.Write newline