Menu Bar of the blog

Sunday, 9 November 2014

Tutorial#4.1: Regular Expressions in VB Script Part-I

Lets take a Test Case,
We need to validate whether Headlines are apearing on a site - www.bbc.com, or not. Now, headlines keep changing daily and hence our object to be validated is dynamic. In such a case, we can use Regular Expressions.
A similar example would be,
Select camera option on eBay site. Check whether prices are being displayed or not on the web page for a number of cameras. To do this, we have a no. of camears being displayed with different models and prices. And this list of cameras keeps changing. To deal with such a large number of dynamic objects, we can use regular expressions.

Now, suppose, we have to find number of occurences of - 'this', in the following string - "this Monday this Tuesday this Wednessday". To solve this, first of all, we will study regexp object.
RegExp Object -
It provides simple Regular Expression support.
Set regex = new regexp
Set is used to define collection of things. So, here, regex is a regexp object having a collection of elements. Regexp object contains a number of properties and methods. The same can also be found at UFT Help -> VB Script Reference -> VB Script Language Reference -> Objects and Collections -> Regular Expressions.

Properties of Regular Expression Object -
  1. Global Property - Sets or returns a Boolean value that indicates if a pattern should match all occurrences in an entire search string or just the first one.
  2. IgnoreCase Property - Sets or returns a Boolean value that indicates if a pattern search is case-sensitive or not.
  3. Pattern Property - Sets or returns the regular expression pattern being searched for.
Methods of Regular Expression Object -
  1. Test Method - It is used to test whether a pattern is pesent in a string or not. If it returns true, then it means pattern has been found in the string.
  2. Execute Method - Executes a regular expression search against a specified string. It returns a collection of objects matched during execution.
So, in our case, we will write the following code:-
str = "this Monday this Tuesday this Wednessday"
var = "this"    'pattern to be matched.
Set regex = new regexp
regex.Global = true
regex.ignorecase = true
regex.pattern = var
If regex.test(str) Then 
   'Matches Collection and Match Object
   Set matches = regex.execute(str)
   print matches.count
   Set match = matches(0)
   print match.firstindex
   print match.value
   Set match = matches(1)
   print match.firstindex
   print match.value

End If
Here, we have created matches as a collection, and match as an object.
The matches collection consists of the objects returned by execute method. These objects are the ones that are present in the string str. Please note that matches is not an array but it is a collection.
The match object consists of different elements contained in the matches collection. Hence, the output of above code will be,
3
0
this
12
this

Here, 3 is the number of times 'this' repeats in our string. 0 and 12 are the first and second index of the pattern. And, 'this' represents the pattern itself.

To iterate through the entire list of matches,
For i = 0 To matches.Count-1
    Set match = matches(i)
    Print "Value found at index - " & match.firstindex & ". Match value is - " & match.value
Next

Here the output will be -
Value found at index - 0. Match value is - this
Value found at index - 12. Match value is - this
Value found at index - 25. Match value is - this


Hence, if we want to make the above code generic, then we can use the following function. The below function can be used for any string or any given pattern.

Function findPatternMatches(str, pat)
   Set regex = new RegExp
   regex.Global = true
   regex.IgnoreCase = true
   regex.Pattern = pat

   print "Pattern Found - " & regex.test(str)
   Set matches = regex.Execute(str)
   print "Total Matches Found: " & matches.Count

   For i = 0 To matches.Count-1
    Set match = matches(i)
    Print "Value found at index - " & match.firstindex & ". Match value is - " & match.value
   Next

End Function
=================************************=================

No comments:

Post a Comment