Wednesday, March 20, 2013

Regular Expressions in C#


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 nondigit 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 nonword 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 “Company
Name: 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. 

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