Wednesday, August 11, 2010

Regular Expression in C#.NET......

Hello Friends,

In our day to day life, developers need to frequently process data and text. The clients and endusers input the data and we need to validate that input data according to our business rules.

A regular expression is a set of characters that can be compared to a string to determine whether the string meets specified format requirements. You can also use regular expressions to extract portions of the text or to replace text. To make decisions based on text, you can create regular expressions that match strings consisting entirely of integers, strings that contain only lowercase letters, or strings that match hexadecimal input. You can also extract key portions of a block of text, which you could use to extract the state from a user's address or image links from an HTML page. Finally, you can update text using regular expressions to change the format of text or remove invalid characters.

How to use Regular Expressions for Pattern Matching?
To enable yourself to test regular expressions, create a console application named TestRegExp that accepts two strings as input and determines whether the first string (a regular expression) matches the second string. The following console application, which uses the System.Text.RegularExpressions namespace, performs this check using the static System.Text.RegularExpressions.Regex.IsMatch method and displays the results to the console:

// C#
using System.Text.RegularExpressions;

namespace TestRegExp
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
if (Regex.IsMatch(args[1], args[0]))
Console.WriteLine("Input matches regular expression.");
else
Console.WriteLine("Input DOES NOT match regular expression.");
}
}
}

Next, run the application by determining whether the regular expression "^\d{5}$" matches the string "12345" or "1234". The regular expression won't make sense now, but it will by the end of the lesson. Your output should resemble the following:

C:\>TestRegExp ^\d{5}$ 1234
Input DOES NOT match regular expression.

C:\>TestRegExp ^\d{5}$ 12345
Input matches regular expression.

As this code demonstrates, the Regex.IsMatch method compares a regular expression to a string and returns true if the string matches the regular expression. In this example, "^\d{5}$" means that the string must be exactly five numeric digits. As shown in Figure 3-1, the carat ("^") represents the start of the string, "\d" means numeric digits, "{5}" indicates five sequential numeric digits, and "$" represents the end of the string.




If you remove the first character from the regular expression, you drastically change the meaning of the pattern. The regular expression "\d{5}$" will still match valid five-digit numbers, such as "12345". However, it will also match the input string "abcd12345" or "drop table customers - 12345". In fact, the modified regular expression will match any input string that ends in any five-digit number.

Also, Kindly find few of randomly used Randomly used Regular Expressions below:
1. Regular Expression for allowing only Integer values:

Allows Negative and Positive Values:
Regex.IsMatch(base.Text, @"^-[0-9]+$|^[0-9]+$");

Allow Only Positive Values:
Regex.IsMatch(base.Text, @"^\d+$");

2. Regular Expression for Float Values:

Allows Negative and Positive Values:
Regex.IsMatch(base.Text, @"^[-]?\d+(?:\.\d{0,2})?$");

Allow Only Positive Values:
Regex.IsMatch(base.Text, @"^\d+(?:\.\d{0,2})?$");

3. Regular Expression for valid Email:

Regex.IsMatch(base.Text, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

4. Regular Expression for valid Internet Address:

Regex.IsMatch(base.Text, @"([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");

5. Regular Expression for valid AlphaNumeric:

Regex.IsMatch(base.Text, @"[a-zA-Z0-9]");



Thanks,
Paras Sanghani

1 comment: