What is Regular Expression ?
Regular expression is a sequence of characters. It means using regular expression object'smethods and properties a pattern can be found and manipulated.
Above description tells us that when to use regular expressions, to list a few major ones
that can be used in Automated Testing with QTP/VBScript:
1. To find a pattern in the content/text etc.
2. To create generic objects in Object Repository
3. To get collection of objects with a particular property pattern in descriptive programming
using regular expressions
Regular Expression (RegExp) Object in VBSript:
Now we are going to explore how to use regular expression object "RegExp". "RegExp" object in VBScript provides regular expression support.Methods of RegExp Object:
1. Execute2. Replace
3. Test
Properties of RegExp Object:
1. Global
2. IgnoreCase
3. Pattern
Using Methods of RegExp Object:
1. Execute Method:
The Execute method returns a Matches collection containing a Match object for each match found in string. Execute returns an empty Matches collection if no match is found.
Syntax:
RegularExpressionObject.Execute(string) 
Here is an instance explaining Execute Method of RegExp Object:
'########################
'@ Execute Method
'########################
Function funRegularExp (patrn, str)
 
 Dim varRgExp, Match, varFound   
    
  Set varRgExp = New RegExp   
  varRgExp.Pattern = patrn
  varRgExp.IgnoreCase = True        
  varRgExp.Global = True     
      
  Set varFound = varRgExp.Execute(str)
  For Each Match in varFound
    varResult = varResult & "Match found at position "
    varResult = varResult & Match.FirstIndex & ". Match Value is '"
    varResult = varResult & Match.Value & "'." & vbCRLF
  Next
  funRegularExp = varResult
End Function
print (funRegularExp("boy", "Boy1 sitting besides the boy2 has reporting card of BoY3"))
The result of above function call with the print statement comes like this:
2. Replace Method:
Replace method of Regular Expression object is used to find a specific pattern in the given string and getting it replaced with provided value
Syntax:
RegularExpressionObject.Replace(string1, string2) 
Here is an instance to illustrate use of Replace method:
'########################
'@ Replace Method
'########################
Function funReplace (patrn, varRepl)
  Dim regEx, sourceStr              
  sourceStr = "The quick brown fox jumped over the lazy dog."
  Set regEx = New RegExp            
  regEx.Pattern = patrn       
  regEx.IgnoreCase = True
  funReplace = regEx.Replace(sourceStr, varRepl)   
End Function
'@ Replace 'fox' with 'rat'.
print (funReplace("fox", "rat"))
The result of above function call with the print statement comes like this:
3. Test Method:
Test method executes a regular expression search in a given string. It returns a Boolean value to specify that pattern is was found in the given string
Syntax:
RegularExpressionObject.Test(string) 
See below example to understand use of regular expression Test method:
'########################
'@ Test Method
'########################
Function funRegularExp (patrn, str)
 
  Dim varRgExp, isFound   
  
  '@ Create New Regular Expression Object
  Set varRgExp = New RegExp
  '@ Set pattern   
  varRgExp.Pattern = patrn
  varRgExp.IgnoreCase = True        
  varRgExp.Global = True     
  
  isFound = varRgExp.Test(str)
  
  If isFound Then
    print "Pattern: "&patrn&" is found in the given string: "&str
  else
    print "Pattern: "&patrn&" is not found in the given string: "&str
  End If
  
End Function
funRegularExp "boy", "Boy1 sitting besides the boy2 has reporting card of BoY3"
Here is the result of above function call:
Use Of Properties of Regular Expression Object:
1. Global Property:
Global property is used to set or return a Boolean value that indicates if a pattern should match all occurrences in an entire search string or just the first one.
Syntax:
RegularExpressionObject.Global [= True | False ]
Global Property Values and DescriptionDefault Value: False
2. IgnoreCase Property:
IgnoreCase property is used to set or return a Boolean value that indicates if a pattern search is case-sensitive or not.
Syntax:
RegularExpressionObject.IgnoreCase [= True | False ]
IgnoreCase Property Values and DescriptionDefault Value: False
3. Pattern Property:
Pattern property is used to set or return the regular expression pattern being searched for.
Syntax:
object.Pattern [= "searchstring"]
