If at first you don't succeed, call it version 1.0

Contact Me

Sarvesh Kushwaha
Email : sarveshkushwaha@outlook.com

Total Pageviews

Powered by Blogger.

Google+ Followers

Saturday, 20 October 2012

Learn Regular Expression in 15-Minutes

What are Regular Expressions and Why we should use it ?

Regular expressions are used to search, replace ,split  ,match  for substrings ("matches") in strings. This is done by searching with "patterns" through the string .

Where To use Regular Expressions ?

Regular Expressions are one of the foundations of the Perl programming language and therefore built-into the compiler itself. Many languages have In-built Regular Expression or they support it via Add-ons.
Microsoft's .NET framework (including C#) , PHP , JAVA, VBScript , JScript , JavaScript ,Typescript, Jquery are languages and libraries which supports regular expressions.

In asp.net you will have to add a namespace to use REGEX .

using System.Text.RegularExpressions;

A very Simple Program to demonstrate REGEX.
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            Regex RE = new Regex("[A-Za-z]{6}");


            Console.ReadLine();  // it will hold your screen untill it gets any input

Above Program will match your string with Upper and lower alphabets and string should be of length Six .output will be True.

Rules and Things To Remember For Regular Expressions -

you can divide regular expression in three parts -

The following table summarizes the Compulsory syntax for regular expressions:

 Sign Description
  ^  It specifies START of Regular Expression.
 $ It specifies the END of Regular Expression.
 [] Square brackets specify the character which needs to be matched.

 Examples : [A-Za-z] means it will only accept Upper and lower   alphabets. [0-9] will accept only numeric numbers .
 {} Curly brackets specify how many characters.

 Examples : {10} means length of string should be 10 digits.{1,4} means {min , max} length of string will be in b/w or equal to min and  max. 
 ()  Round brackets specify grouping.

Example :

now lets test what we have learned .create a regular expression for indian mobile number validation .To create mobile number validation what we know , that mobile number should be of 10 numeric digits.so regular expression will be -

above expression tells that string should be only numeric i.e [0-9].and {10} fixed digits.but still this expression can take 0123456789 .that is not a valid mobile number , because mobile number can't start from zero.so lets enhance this regular expression.


now first digit should be in b/w 1 to 9 .now make this expression more perfect group your digits with round brackets : ^([1-9]{1})([0-9]{9})$

Shortcuts and Quantifiers :

 Shorcuts/Quantifier Description
 * Zero or more matches.
  .  Matches any character except \n
 + One or more matches.
 ? Zero or one matches.
  \d  Matches any decimal character
 \w Matches any alphanumeric character and underscore.

to learn in depth about  REGEX please go to this link : Cheat Sheet for Refular Expression,


  1. Nice tutorial. Quite helpful.

    Can you come up with reqex in UNIX and PERL also?

  2. Thanks guru ji .... sorry don't have knowledge about unix .. hoping to learn above mentioned regex then surely will write . :)

  3. Thanks for Your ideology in Regular expression .


  4. Thanks for Your ideology in Regular expression .


  5. "I very much enjoyed this article.Nice article thanks for given this information. i hope it useful to many pepole.php jobs in hyderabad.

  6. • Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updatingAzure Online Training