Regular Expressions
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.
Which uses the System.Text.RegularExpressions namespace, performs this check using the static method System. Text.RegularExpressions.Regex.IsMatch method
How to Use Regular Expressions for Pattern Matching
Example:-
“^\d{5}$” => means that the string must be exactly five numeric digits.
"^" => Match beginning of input
"\d" => Match only numeric digits
"{5}" => Match exactly 5 characters
"$" => Match end of input
Regular expressions can be used to match complex input patterns, too.
Regular expressions are an extremely efficient way to check user input,
However, Regular expressions are difficult to create unless you are extremely familiar with the format.
"\d" => "[0-9]" => Matches a digit character.
"\d{3}" => "[0-9]{3} => Matches 3 digit character.
(< 3 and > 3 are Invalid)
"\d{2,5}" => "[0-9]{2,5} => Matches 1 to 5 digit character
(< 2 and >5 are Invalid)
"\D" => "[^0-9]" => Matches a non digit character.
"\s" => "[\f\n\r\t\v]" => Matches any white-space character.
(Space, Tab and form-feed)
"\S" => "[^\f\n\r\t\v]" => Matches any non-white-space character.
"\w" => "[A-Za-z0-9]" => Matches any word character including underscore.
"\W" => "[^A-Za-z0-9]" => Matches any non word character, including underscore.
How to Extract Matched Data
Besides simply determining whether a string matches a pattern, you can extract information from a string. For example, if you are processing a text file that contains “CompanyName: Contoso, Inc.”, you could extract just the company name using a regular expression.
To match a pattern and capture the match, follow these steps:
1. Create a regular expression, and enclose in parentheses the pattern to be matched.
2. Create an instance of the System.Text.RegularExpressions.Match class using the static Regex. Match method.
3. Retrieve the matched data by accessing the elements of the Match. Groups array.
EXAMPLE 1
string input = "Company Name: Contoso, Inc.";
Match m = Regex.Match(input, @"Company Name: (.*$)");
MessageBox.Show (m.Groups[1].ToString());
EXAMPLE 2
Email Phone number validation
Regex r = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
Regex r2=new Regex( @"^\d{4}(\-\d{7})?$");
// @"^\(?(\d{3})\)?[\s\-]?(\d{3})\-?(\d{4})$"); international code
if (r.IsMatch(txtMail.Text))
{
MessageBox.Show("Valid");
}
else
{
MessageBox.Show("Invalid Email");
}
if(r2.IsMatch(textBoxPhNo.Text))
{
MessageBox.Show("Valid");
}
else
{
MessageBox.Show("Invalid phone number");
}
EXAMPLE 3
3 DIGIT NUMBER VALIDATION
Regex x = new Regex(@"^[0-9]{1,3}$");
// Regex x = new Regex(@"^\d{3}$");
if (x.IsMatch(txtNo.Text))
{
MessageBox.Show("Valid");
}
else
{
MessageBox.Show("Invalid");
}
Summary
■ Regular expressions enable you to determine whether text matches almost any type of format. Regular expressions support dozens of special characters and operators. The most commonly used are “^” to match the beginning of a string, “$” to match the end of a string, “?” to make a character optional, “.” to match any character, and “*” to match a repeated character.
■ To match data using a regular expression, create a pattern using groups to specify the data you need to extract, call Regex .Match to create a Match object, and then examine each of the items in the Match. Groups array.
0 comments:
Post a Comment