.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

B- Ball, C- Cat, D - dog: - Learning (REGEX) regular expression the easy way.

Posted By:Shivprasad koirala       Posted Date: March 23, 2011    Points: 200    Category: .Net Framework    URL: http://www.questpond.com  

Regex has been the most popular and easiest way of writing validations. The only big problem with regex has been the cryptic syntax.
 

B- Ball, C- Cat, D - dog: - Learning (REGEX) regular expression the easy way.


Contents

So, what's the agenda?
Just in case if you are new comer, what is regex?
3 important regex commands
Check if the user has entered shivkoirala?
Let's start with the first validation, enter character which exists between a-g?
Enter characters between [a-g] with length of 3?
Enter characters between [a-g] with maximum 3 characters and minimum 1 character?
How can I validate data with 8 digit fix numeric format like 91230456, 01237648 etc?
How to validate numeric data with minimum length of 3 and maximum of 7, ex -123, 1274667, 87654?
Validate invoice numbers which have formats like LJI1020, the first 3 characters are alphabets and remaining is 8 length number?
Check for format INV190203 or inv820830, with first 3 characters alphabets case insensitive and remaining 8 length numeric?
Can we see a simple validation for website URL's?
Let's see if your BCD works for email validation?
Short cuts
Quick references for regex


So, what's the agenda?

Regex has been the most popular and easiest way of writing validations. The only big problem with regex has been the cryptic syntax. Developers who are working on projects with complicated validation always refer some kind of cheat sheet to remember the syntaxes and commands.
In this article we will try to understand what regex is and how to remember those cryptic syntaxes easily.
 

 

You can watch my .NET interview questions and answers videos on various sections like WCF, Silver light, LINQ, WPF, Design patterns, Entity framework etc
 

Just in case if you are new comer, what is regex?

Regex or regular expression helps us describe complex patterns in texts. Once you have described these patterns you can use them to do searching, replacing, extracting and modifying text data.

Below is a simple sample of regex. The first step is to import the namespace for regular expressions.

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}");

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("shivkoirala").ToString());


3 important regex commands


The best way to remember regex syntax is by remembering three things Bracket, carrot and Dollars.
 

 

B There 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.

C Carrot "^" the start of a regular expression.
D Dollar "$" specifies the end of a regular expression.
 


Now once you know the above three syntaxes you are ready to write any validation in the world. For instance the below validation shows how the above three entities fit together.
 

 

. The above regex pattern will only take characters which lies between 'a' to 'z'. The same is marked with square bracket to define the range.

. The round bracket indicates the minimum and maximum length.

. Finally Carrot sign at the start of regex pattern and dollar at the end of regex pattern specifies the start and end of the pattern to make the validation more rigid.

So now using the above 3 commands let's implement some regex validation.


Check if the user has entered shivkoirala?

shivkoirala

Let's start with the first validation, enter character which exists between a-g?

[a-g]

Enter characters between [a-g] with length of 3?

[a-g]{3}

Enter characters between [a-g] with maximum 3 characters and minimum 1 character?

[a-g]{1,3}

How can I validate data with 8 digit fix numeric format like 91230456, 01237648 etc?

^[0-9]{8}$

How to validate numeric data with minimum length of 3 and maximum of 7, ex -123, 1274667, 87654?

We need to just tweak the first validation with adding a comma and defining the minimum and maximum length inside curly brackets.

^[0-9]{3,7}$


Validate invoice numbers which have formats like LJI1020, the first 3 characters are alphabets and remaining is 8 length number?

First 3 character validation
^[a-z]{3}
8 length number validation
[0-9]{8}


Now butting the whole thing together.

^[a-z]{3}[0-9]{7}$


Check for format INV190203 or inv820830, with first 3 characters alphabets case insensitive and remaining 8 length numeric?

In the previous question the regex validator will only validate first 3 characters of the invoice number if it is in small letters. If you put capital letters it will show as invalid. To ensure that the first 3 letters are case insensitive we need to use ^[a-zA-Z]{3} for character validation.

Below is how the complete regex validation looks like.

^[a-zA-Z]{3}[0-9]{7}$


Can we see a simple validation for website URL's?


Steps

Regex
Step 1 :- Check is www exist
^www.
Step 2 :-The domain name should be atleast 1 character and maximum character will be 15.
. [a-z]{1,15}
Step 3 :-Finally should end with .com or .org
. (com|org)$

 

^www.[a-z]{1,15}.(com|org)$


Let's see if your BCD works for email validation?
 


Steps

Regex
Step 1 :-  Email can start with alphanumeric with minimum 1 character and maximum 10 character. , followed by at the rate (@)
^[a-zA-Z0-9]{1,10}@
Step 2 :-The domain name after the @ can be alphanumeric with minimum 1 character and maximum 10 character , followed by a "."
[a-zA-Z]{1,10}.
Step 3 :-Finally should end with .com or .org
.(com|org)$

 

^[a-zA-Z0-9]{1,10}@[a-zA-Z]{1,10}.(com|org)$


Short cuts

You can also use the below common shortcut commands to shorten your regex validation.

Actual commands Shortcuts
[0-9]   \d
[a-z]   \w
O or more occurrences   *
1 or more occurrences   +
0 or 1 occurrence   ?

 

Quick references for regex

Great concise cheat sheet http://www.dijksterhuis.org/csharp-regular-expression-operator-cheat-sheet/


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend