Regular Expressions, Regex

Regular Expressions

Regular Expressions are often used in C# with the help of a regex class. Most of the times we need to process data in forms which is related to a particular person. The data may be related to that person’s job, or it can have important details related to that person, for instance, his residence information, his date of birth, his marital status, his gender and so on. C# provides some standard syntax for introducing Regular Expressions in a C# program or matching or searching those regular expressions. For using Regular Expressions in our C# program first.

Includes for Regular Expression

We must have to introduce the library of regular expressions in this manner.

Using Regular Expression

Now if you want to use an array of string in your program and the data for that string is pre-defined then you have to do the following steps.

You have to define the type of array you want to use. If the type of array is of string type then type:

Now after that you have to define or initialize the variable in which you want to save the array in this manner:

Similar to Java you have to make the instance of that variable array in this manner:


This new string[3] will tell your program when it is executed to fetch data from an array of string type, and the size of that array is 3 and String[] expressions will tell the program that this array is saved in a variable named as the expression of string array type.
Now if you have a pre-defined set of data of string, then you will initialize that data with each index of your array in the following manner.

Email Validation with Regular Expression

Let us consider that we have some pre-defined email for some people. Now we can relate those e-mails with each index of array in the following way:


This statement Expression[0] will point to the 0 index of your variable of array type. The statement =””; will assign the mail mentioned to Expression[0].
In the same manner, you can assign some more mail addresses to other indexes of your variable Expression, which is of array type. Like

For accessing the data set elements which are pre-defined by you manually to each index of the array you will have to follow the following steps.
First, you have to make an instance of Regex class which is a built-in library for regular expressions like this:

You must assign the pattern of data which you have defined against each index of your array variable like this:

After new Regex you have to introduce @ in () before you start defining the syntax of you input because of two reasons among which the 1st reason is that it tells the compiler during compilation that you cannot ignore any part of the string data enclosed in quotation marks  “”. \w is for matching any character in your string. It allows you to have any number or alphabet in your string. \d strict that you must have one alphanumeric character in your string like 2a 43d.

Alphanumeric represents the combination of numbers and letters. [\w\d] means that from the start of your string expression, the first character must be an alphabet or a number but the second character or set of characters must be alphanumeric. +@[\w] means to add another set of strings or numbers in your data, and means that your string must end with .com

As you have an array variable initialized. So for accessing the data items in the array you have to use a for loop which will continue according to the size you have mentioned in [3]. Now as the size is 3. So, your loop will continue looping for three times. You have to type the following statement.

Matching Expression with Regular Expression

Now inside the curly brackets if you want to match your pre-defined data string sets that if they are changed or not then you will type the following statements:

When the loop continues running then it will check the following condition

In regex, the pattern of the data sets defined by you will be saved or in other words, the metadata of your data will be saved in the regex. Now IsMatch() is a built-in function of c# which matches the metadata or any query on the basis defined in the condition statement. Now in the case mentioned above, the base is the regex. expression[i] is the array variable of type string, and it will indicate the index on which your data is stored. In this way the IsMatch() function will match each element of your data set index by index stored in expression[i] with regex in which your metadata is stored.

If Is Match() function is successful in matching your data element with regex then this statement


This will display the data element matched. Console means that the interface on which the output will be displayed will be a command line Interface, not a GUI(Graphical User Interface). WriteLine() is a built-in function in c# just like cout<< in c++ which is used to display output on the screen. “Expression” will be displayed as it is on the screen.+expression[i] means to add the data which is matched with the output and +”matches” means to add matches with the output.

2nd Method to use Regex for Email Validation

Now if you want the user to enter his or her email, then you have to use the following method.
First, you have to define or initialize the variable in which the e-mails of the users will be saved, and as the email is of type string then you have to initialize your variable in the following manner as mentioned above:
String[] expression= new string[3];
Then you have to follow the following step:



Single Date Items (Numeric Validation)

1st Method for Single Data Item:

If you only have one data item and the data item is pre-defined then you have to deal with it in the following manner.
First, you have to initialize a variable with the available data you have like this:
String expression=”10-10-2015″;
Now you have to make instance of Regex class in which you will define the pattern of your data like this:


0[1-9] means numbers from 01-09.
1[0-9] means numbers from 10-19
0[1-9]|1[0-9] means numbers from 0[1-9] data set |(or) numbers from 1[0-9] data set.
[-] is a separator
Then you can follow the following step.


2nd Method for Single Data Item:

If you want the user to enter data, then you have to follow the following steps.
String expression;

Leave a Reply

Your email address will not be published. Required fields are marked *