In this video we will try to demonstrate what are regular expression
regular expressions are also called as Regex
We will see come practical demonstration of how to use regular expressions in .NET
First will start with the definition of Regular expression. Regular expression helps to describe complex patterns in text
One we define this complex patterns then we can search the text, replace, extract and modify the text data
Every structure data has a specific pattern
Will considered a simple example of a website url
There is a very structured patter by which we define a website url
The website url is divided into three parts, the first one is "www"
the second one is the domain name
the third one is a com, in, org the extension of the domain
Each of these three parts are later separated by using the dots(.)
In the same way if we look at email address it also has a specific pattern
or some kind of Invoice number which is marked by the date and the years
or even a simple data type which has a MM/DD/YY format or YY/MM/DD format. Every structure data type has a pattern
Regular expression helps us to define this patterns
Once we define this pattern then we can do validations, searching, modifying the text etc
The biggest issue with the regular expressions or regex, is it not easy to define these patterns
Above is a example of regular expression for email address
First will try to understand the basics
syntax of writing regular expression and then will start with the practical demonstration
There are three kinds od symbols
which can help us to define any kind of
complex pattern or regular expression
Brackets
Carrot sign
and the Dollar sign
i.e. the B, C and D
In the brackets we will be using all the three kinds of brackets i.e. the square bracket, the curly bracket and the round brackets
Will come each one of these brackets and use them as we will do practical demonstration
The carrot and the dollar sign will specify the start and the end of the regular exression
Over all regular expression is structured by using B, C and D is
The start and the end will be specified by using carrot sign and the dollar sign
while in between will have the square brackets and round brackets defining the regular expression
Will start with the practical demonstration and how to B, C and D to specify any kind of regular expression or pattern
To understand regular expression
we have created simple windows form with two text boxes
One text box is for data
and the other text box is for pattern
This text box we named it as textData
and there is a other text which we named as txtPattern
On the same form we have validate button
When we click on the validate button
whatever data we have provided here
will be validated against the regex pattern which we have written on the text box
The first thing we need to do using regular expression is
import the namespace System.Text. RegularExpressions;
that is the first step
The second step is we need to create the regex object
and specify the pattern in the constructor of the regex class
We have created a regex object and specified the pattern
Currently the pattern will specify is on txtPattern
then
we will specify the txtData in the IsMatch function
Whatever object we have created of the rejex class
there is a IsMatch function
We will provide
the data
on this function
It will return a false and true depending on how this data matches the pattern
We have created the project
now will start taking the above validations one by one
and how we can create them
or how we can write the regular expression pattern for them
Start with the first validation
Enter characters
which exist between a and g
Do a Control+F5
Ensure that the end users puts the data between a to g
Start with the square bracket
Inside the square bracket
the range is between a to g and the square bracket
This is the pattern
If we put here a "x" it will give validate False
If we try to put n it will give False
If we put b
it will give true
If we put valid data it will give as true or as false
It is taking this pattern and validating the pattern with the data
Enter characters between a to g with length of 3
This validation is very much similar to the precious validation
with the only variation we will validate it for the length of 3
In the previous validation we had a problem
The validation of a-g is only done for one character
If we put a it does validated
But the time we say a
and then we say z
it still gives us True which is wrong because there is a character called as z which is outside the boundary of a-g
How to specify
a length for this validation?
To specify the length of the validation we can use
the curly brackets and say this validation has to be stretch for 3 characters
or 3 length
If we hit the validate now, it will give us False
If we say abc it is True
If we put z
it will give False
To specify the length for the validation we need to use the Curly brackets
The square bracket specify what characters we want in the
data
for which the validation will be true
and the curly brackets to specify the length of the validation
Still we have not solved the main problem
in this validation
the length is 3
if we put here abc and hit validate it gives True which is right
after the c if we put z
it still gives true which is wrong
because here it should show
this is False because z is not allowed here
We have specified the length of 3
This happening because the validation is not Regit or the Regex what we have specified is not Regit
To make the regular expression more regit
we can mark the regular expressions with the start and the end sign i.e. with the carrot
and a dollar sign
By marking the start and the end with the carrot and dollar sign
we specify
this is a regit regular expression
If we hit the validate button
it gives us False
Any kind of length which is above
3 characters
and if it is not in the range of a-g
it will give us False
The carrot and the dollar signs makes the regular expression more regit
The C for the Carrot
the D for the Dollar
and in between we will have the bracket signs i.e. the square bracket as well as the curly brackets
We have done two practicals
the first one where we said the character should only fall in between a-g
and the second one is
the character should fall between a-g with the length of 3 characters
Now will move ahead to the third validation where we say again the same thing
check for the character between a-g
but the maximum length should be and the minimum length should be 1
Start with the carrot sign which specifies the start of the regular expression
then specify the square bracket a-g
then start with the curly brackets. In the curly brackets we can also specify
the minimum length of the validation or of the data
1 specifies that the minimum length of this validation is 1
or this data is 1 and the maximum is 3
and specify the end of the validation
If we put here nothing then it will give us False
The minimum length is 1
If we specify here a
it will give me true because
the length is 1
and it is lying in between a-g. If we put z it gives us false
If we put abc it is True
because the length abc lies in between a-g
and the
length is 3
If we try to put here
d
It will give us False
To specify the maximum characters in minimum characters we can use the curly brackets
The first parameter the curly brackets specifies the minimum characters
and the second parameter of the curly bracket specifies the maximum parameters
How to validate data with 8 digit fix numeric format like 01237648
Start with the carrot sign as usual
The way we specified range for alpha numeric for characters. In the same ways we can specify the range for numeric 0 to 9
This validation, the data should be only between 0 to 9, it should be of 8 length
and end of the
validation
or the regular expression. If we put here anything
it gives us False
If we put here 123, again it is false because the length is not 8
If we go more than 8 it will give us false
To specify the range which is almost the next question how to validate numeric data with minimum length of 3 and maximum length of 7
Put here 3,7
If we put here 123 it will give us True
and anything below
3 length it will give False
anything till 7
will give us True
Anything above 7
again gives us
validation as false
Now will try with next example Validate invoice numbers which have formats like LJI1020, where the first 3 letters are characters
and the remaining are
numerics of 8 length
First start with the carrot sign
and end with the dollar sign
a-z
of length 3
and after that 0-9
of
length
8
If we type here inv12345678
this will give us True
If we remove this
it will give us False
If we just put 1 here it give us False
If we put scr
it will give us True
We are able to define
a invoice number which has format of 3 characters
and the numeric length is 8
The problem here is if we type here
Capital
it gives validate False which is not right
If we say
PQR12345678
this should be true because
the first three are characters and the rest ones are numerics
a-z is only for small, we need to say a
-z.Also
take the capitals
This will give us true
Either a-z small or either A-Z capital
That is our next question, Check for the invoice number format with first 3 characters alphabet case either capital or either small
Can we see a simple validation for website URL's. When we normally see a website url, the structure is something like www.questpond.com
Its always start with www with dots then then the domain name followed by .com, .org, .in, .us
Put the carrot sign first and the $ sign to ensure this is a close ended regular expression and would like the www. as it is
The domain name can be alpha numeric values with a-z
0-9
and also capital letters
The size can be vary from 1 to 10
domain names can go above 10 but for now it is 10
We can have various domain extensions like .com, .org, .us, .in
This is an or condition (the pipe)
as we have in C#
the round brackets signifies this is a group and there can be an and condition or or condition. It is meant for grouping purpose
It's true
Will remove com
it's false
Will say www.quest123.com(this is true) because it can have numerics
Validate is the simple website URL
Can we see simple email validation?
Send us the solutions/ answers at questpond@questpond.com, will reply back if the validation for email is proper or not
In this tutorial we saw what is regex and how to remember regular expression in a easy way
and we saw some very small and nice concise practical demonstration to understand regular expression in a proper way
Thank you very much