Regular expressions are the combination of wild cards and characters which is used to perform pattern matching.
It is string that provides a complex search phrase(is an expression consisting of one or more words.)
Uses of Regular expression is Scripting.
1. Test for a pattern withing a string - To check for existing of substring is a string. For example we can test an input string to see if a telephone number pattern or a credit card number pattern occurs withing the string, it is called data validation.
2. Replace Text - To find and replace a string with another string. We can use a regular expression to identify specific text in a document and either remove it completely or replace it with other text.
3. Extract a substring from a string based upon a pattern match- To get a string based on pattern match. we want all the words starting with "A" from a document. In this case we will use regular expression which will create pattern match and will return all words starting with "A".
Note - Regular Expression can be used to find the text strinng valus and objects with varying values. Regular Expression will be helpful when the expected value of any object or string is regularly changing.
In Vb-script we have "RegExp" class which has the following properties and methods.
Properties -
1. Global - It is used to specify pattern matching on given text until end of text.
2. Ignore Case - It is used to specify case sensitiveness.
3. Pattern - It is used to store expected expression for matching.
Methods -
1. Execute - It is used to apply given pattern on givin text and find matched values in text.
2. Replace - It is used to replace matched text with other text.
3. Test - It is used to apply given pattern on given text for matching. If pattern was matched with text, then this method returns true.
Properties Syntax:-
Pattern: It is used to store expected expression for matching.
Syntax:-
set r=new regexp
r.pattern="\w"
Global: It is used to specify pattern matching on given text until end of text.
Syntax:-
set r=new regexp
r.pattern="\d"
r.global=true\false
IgnoreCase: It is used to specify case sensitiveness.
Syntax:-
Set r=new regexp
r.pattern="[A-Z][a-z]"
r.global=true
r.ignorecase=true
Methods Examples:-
Execute():-It is used to apply given pattern on given text and find matched values in text.
Example:-
Set rg=New RegExp
rg.Pattern="[0-5]+"
rg.Global=True
String1="mno343de5rfdsdfsd105dsfjdk"
Set x=rg.Execute(string1)
For Each y In x
MsgBox y.value
Next
Output-
343
5
105
Test:- It is used to apply given pattern on given text for matching, if pattern was matched with text, then this method returns true.
Syntax:-
Set r=New RegExp
r.Pattern="[0-6]+"
r.Global=True
x="34218943124981"
If r.Test(x) Then
MsgBox "Data is matched"
Else
MsgBox "Mismatch in data"
End If
OutPut-
Data is matched
WildCards-
. - Dot matches with any single characters.
Example- A.B -- A2B, APB, A B, A&B, A. B.
{n}- It matches with, n occurrences of previous characters.
Examples-
Yaho{2}----- Yahoo
Go{3}gle---- Google
(Zup){3}------ Zup Zup Zup
{n,m}- It matches with minimum 'n' occurrences and maximum 'm' occurrences of previous characters.
Examples-
Yaho{0,2} ----- Yah
Yaho
Yahoo
(Ding){1,3}---- Ding
Ding Ding
Ding Ding Ding
? - It matches with Zero(0) or 1 concurrences of previous character.
Examples-
Dinga{0,1}---- Ding
Dinga
+ - It matches with one or more occurences of previous character which is same as {1, infinite}
Examples-
Yaho{1, }-- Yaho
Yahooo
Yahooooooooooooooooooo...................................
Yaho+ --- Yaho
Yahooo
Yahoooooooooooooo...........................................
* - It matches with zero of more occurence of previous character, which is same as {0, infinite}.
Examples-
Yaho* ---- Yah
Yaho
Yahooooooooooooooooooo...........................................
X|Y - It matches with either X(Complete Left Side) or Y(Complete Right Side).
Examples--
F|Wood -- F
Woood
(F|W)ood----- Food
Wood
(R|C|B)at ----------Rat
Cat
Bat
[ABC] - It matches with any one of the character.
Examples--
[RCB]at ----- Rat
Cat
Bat
Emp[A&B] --- Emp A
Emp &
Emp B
[A- Z] - It matches with any one of the letter in the range.
Examples -
Emp [D - H ] ----- Emp D
Emp E
Emp F
[0 - 9] - It matches with any one of the digit in the range .
Examples-
Emp[2 - 5] - Emp 2
Emp 3
Emp 4
Emp5
It is string that provides a complex search phrase(is an expression consisting of one or more words.)
Uses of Regular expression is Scripting.
1. Test for a pattern withing a string - To check for existing of substring is a string. For example we can test an input string to see if a telephone number pattern or a credit card number pattern occurs withing the string, it is called data validation.
2. Replace Text - To find and replace a string with another string. We can use a regular expression to identify specific text in a document and either remove it completely or replace it with other text.
3. Extract a substring from a string based upon a pattern match- To get a string based on pattern match. we want all the words starting with "A" from a document. In this case we will use regular expression which will create pattern match and will return all words starting with "A".
Note - Regular Expression can be used to find the text strinng valus and objects with varying values. Regular Expression will be helpful when the expected value of any object or string is regularly changing.
In Vb-script we have "RegExp" class which has the following properties and methods.
Properties -
1. Global - It is used to specify pattern matching on given text until end of text.
2. Ignore Case - It is used to specify case sensitiveness.
3. Pattern - It is used to store expected expression for matching.
Methods -
1. Execute - It is used to apply given pattern on givin text and find matched values in text.
2. Replace - It is used to replace matched text with other text.
3. Test - It is used to apply given pattern on given text for matching. If pattern was matched with text, then this method returns true.
Properties Syntax:-
Pattern: It is used to store expected expression for matching.
Syntax:-
set r=new regexp
r.pattern="\w"
Global: It is used to specify pattern matching on given text until end of text.
Syntax:-
set r=new regexp
r.pattern="\d"
r.global=true\false
IgnoreCase: It is used to specify case sensitiveness.
Syntax:-
Set r=new regexp
r.pattern="[A-Z][a-z]"
r.global=true
r.ignorecase=true
Methods Examples:-
Execute():-It is used to apply given pattern on given text and find matched values in text.
Example:-
Set rg=New RegExp
rg.Pattern="[0-5]+"
rg.Global=True
String1="mno343de5rfdsdfsd105dsfjdk"
Set x=rg.Execute(string1)
For Each y In x
MsgBox y.value
Next
Output-
343
5
105
Test:- It is used to apply given pattern on given text for matching, if pattern was matched with text, then this method returns true.
Syntax:-
Set r=New RegExp
r.Pattern="[0-6]+"
r.Global=True
x="34218943124981"
If r.Test(x) Then
MsgBox "Data is matched"
Else
MsgBox "Mismatch in data"
End If
OutPut-
Data is matched
WildCards-
. - Dot matches with any single characters.
Example- A.B -- A2B, APB, A B, A&B, A. B.
{n}- It matches with, n occurrences of previous characters.
Examples-
Yaho{2}----- Yahoo
Go{3}gle---- Google
(Zup){3}------ Zup Zup Zup
{n,m}- It matches with minimum 'n' occurrences and maximum 'm' occurrences of previous characters.
Examples-
Yaho{0,2} ----- Yah
Yaho
Yahoo
(Ding){1,3}---- Ding
Ding Ding
Ding Ding Ding
? - It matches with Zero(0) or 1 concurrences of previous character.
Examples-
Dinga{0,1}---- Ding
Dinga
+ - It matches with one or more occurences of previous character which is same as {1, infinite}
Examples-
Yaho{1, }-- Yaho
Yahooo
Yahooooooooooooooooooo...................................
Yaho+ --- Yaho
Yahooo
Yahoooooooooooooo...........................................
* - It matches with zero of more occurence of previous character, which is same as {0, infinite}.
Examples-
Yaho* ---- Yah
Yaho
Yahooooooooooooooooooo...........................................
X|Y - It matches with either X(Complete Left Side) or Y(Complete Right Side).
Examples--
F|Wood -- F
Woood
(F|W)ood----- Food
Wood
(R|C|B)at ----------Rat
Cat
Bat
[ABC] - It matches with any one of the character.
Examples--
[RCB]at ----- Rat
Cat
Bat
Emp[A&B] --- Emp A
Emp &
Emp B
[A- Z] - It matches with any one of the letter in the range.
Examples -
Emp [D - H ] ----- Emp D
Emp E
Emp F
[0 - 9] - It matches with any one of the digit in the range .
Examples-
Emp[2 - 5] - Emp 2
Emp 3
Emp 4
Emp5