Tuesday, 23 July 2013

Insight Regular Expression.

Note: All this data has been copied from various sources for the purpose of knowledge sharing. Data content  can be either as it is or with little modification.
---------------------------------------------------------------------------------------------------------------------

Namespace for regular expression is
using System.Text.RegularExpressions;

The next thing is to create the regex object with the pattern. The below pattern specifies to search for alphabets between a-z with 10 length.
Regex obj = new Regex(“[a-z]{10}”);

Note: All this data has been copied from various sources for the purpose of knowledge sharing. Data content  can be either as it is or with little modification.
---------------------------------------------------------------------------------------------------------------------

Finally search the pattern over the data to see if there are matches. In case the pattern is matching the ‘IsMatch’ will return true.
MessageBox.Show(obj.IsMatch(“Andrew1234”).ToString());

regex syntax is mainly depend on use of bracket, caret and dollar. 


BThere are 3 types of brackets used in regular expression
Square brackets “[“and Curly “{“ brackets.
Square brackets specify the character which needs to be matched while curly brackets specify how many characters. “(“ for grouping.
We will understand the same as we move ahead in this article.
Ccaret “^” the start of a regular expression.
DDollar “$” specifies the end of a regular expression.

Example :
^[a-e]{1,2}$ 
explains a expression with aphabets between a and e with minimum size of 1 and maximum size of 2

Validate invoice numbers which have formats like LJI1020, the first 3 characters are alphabets and remaining is 8 length number?
^[a-z]{3}[0-9]{8}$

make above validation case insensitive
^[a-zA-Z]{3}[0-9]{8}$

Simple validation of website URL



No comments:

Post a Comment