Regular Expressions in VBScript or QTP or UFT

2
401
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Regular Expressions in QTP/UFT/VBScript
Regular Expressions in QTP/UFT/VBScript
Regular Expressions in QTP

This post talks about regular expressions in vbscript,regular expressions in UFT,regular expressions in vbscript examples,regular expressions in UFT examples,using regular expressions in vbscript,using regular expressions in UFT,VBScript RegExp Object,UFT RegExp Object,regular Expression,How to use regular expression.

VBScript does not support regular expression constant (/a pattern/ ). VBScript provides pattern properly of a RagExp object.

Regular Expressions in QTP/UFT/VBScript:

What is Regular Expression?

It is a way of representing data using symbols. They are often used within matching, searching or replacing algorithms.Regular expressions enables us to perform the following tasks.

  • Pattern matching and replace.
  • Searching and replace.
  • Replace
  • Identifying required number or string

Text strings get assigned to pattern property of a RegEx object.

Regular Expressions in QTP/UFT:

Regular expressions can be used in QTP/UFT for identifying objects and text strings with varying values.

Where we use regular expression:

o Defining the property values of an object in Descriptive programming for  handling dynamic objects
o For parameterizing a step
o creating checkpoints with varying values

Using Regular Expressions in QTP or UFT:

We can define a regular expression for a constant value, a Data Table parameter value, an Environment parameter value, or a property value in Descriptive programming.

We can define a regular expression in standard checkpoint to verify the property values of an object; we can set the expected value of an object’s property as a regular expression so that an object with a varying value can be verified.

We can define the text string as a regular expression, when creating a text checkpoint to check that a varying text string is displayed on our application,
For XML checkpoints we can set attribute or element values as regular expressions.

RegExp object

VB Script is providing RegExp object for defining Regular expressions, It provides simple support for defining regular expressions. RegExp comes with predefined properties and methods.
Regular Expression Object Properties and Methods:
Properties:
a) Global Property
b) IgnoreCase Property
c) Pattern Property
Methods:
a) Execute() Method
b) Replace() Method
c) Test() Method

Global Property

Global property accept two values.

  • True– it signifies the search will happen to the entire string.
  • False– searches for the first occurrence

Syntax

Object.Global= value

By default global property is set to false.The Boolean value determines whether all occurrence of a pattern should be replaced.

Example with /b switch and without /b switch

IgnoreCase Property

This is helpful when we want to search a text.(In case – insensitive manner).Ignore case property accept two values.

  • True– it will not ignore the case while searching
  • False– it will make the search case sensitive

syntax-

Object.IgnoreCase= value

The Boolean value determines whether a case sensitive search needs to be performed.

Dim reg, strString
Set reg=New RegExp
reg.Pattern="/b of" //(/b matches the exact word)
reg.Global=True
reg.IgnoreCase=True
strString="To take OF is not easy of the ground."
msgbox reg.Replace(strString,"off")

O/P- To take off is not easy of the ground.

Pattern Property

The pattern property takes the flags and text pattern that needs to be searched.This property sets or returns the regular expression that needs to be searched.

syntax-

Object.Pattern=[(flag info)("Search String/search info")]

The argument can be a string or a regular expression itself. Flag info is optional here.

Regular Expressions Examples or How to use Regular Expression?

  1. We need to create a new regular expression object
    set myVar=New RegExp
  2. We need provide the key that needs to be matched.
    myVar.pattern="[[mySearchText]]"
  3. myLine-  “the line or string from where the matching will happen”
  4. Ask VBscript engine to perform the pattern matching and perform your task.
    msgbox myVar.Replace( myLine,[[text after replacement]])

Example of Regular Expression

Dim myVar,myLine
Set myVar=New RegExp //regular expression object
myVar.Pattern="boy" //creates pattern
myLine="every boy is great" //initial line
msgbox myVar.Replace( myLine,"girl")

o/p- every girl is great

How to make it dynamic?

We can make it dynamic by introducing an input box.regular expressions in vbscript examples

Dim myVar, myLine, myWord
Set myVar=New RegExp
myLine= inputbox(" Enter your line")
myVar.pattern= inputbox(" enter a word to change")
myWord= inputbox(" enter a word that will be final")
msgbox  myVar.replace( myLine, myWord)

For more dynamic behaviour we can create a function, it will help us achieve CD-CT-CI.
It will also help in unattended execution.

regular expressions in vbscript examples/VBScript RegExp Object

Function ChangeLine(sLine, sWordToReplace, sNewWord)
Dim myVar,myLine,myWord
Set myVar==New RegExp
myLine=sline
myVar.Pattern= sWordToReplace
myWord=sNewWord
ChangeLine=myVar.Replace( myLine, myWord)
End function

The above example will only change the first occurrence of the word in the line. But if the line has many words( that needs to be changed) it will not work fully.We need to change the script little bit.

myVar.Pattern= "switch and the search word"
myVar.Global= True

Switch mainly used is /b (word boundary)
The Global property ensures match in all occurrence of the word.

1) Match File Names in a Directory against Regular Expression

regular expressions in vbscript examples/VBScript RegExp Object

Set objFS = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strCurrentDirectory = objShell.CurrentDirectory
Set objFolder = objFS.GetFolder(strCurrentDirectory)
Set colFiles = objFolder.Files
Set objRE = New RegExp
objRE.Global     = True
objRE.IgnoreCase = False
objRE.Pattern    = WScript.Arguments(0)
For Each objFile In colFiles
bMatch = objRE.Test(objFile.Name)
If bMatch Then
WScript.Echo objFile.Name
End If
Next

2) Match Content in a File against a Regular Expression

regular expressions in vbscript examples/VBScript RegExp Object

strFileName = "E:test.txt"
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objTS = objFS.OpenTextFile(strFileName)
strFileContents = objTS.ReadAll
WScript.Echo "Searching Within: "
WScript.Echo strFileContents
objTS.Close
Set objRE = New RegExp
objRE.Global     = True
objRE.IgnoreCase = False
objRE.Pattern    = WScript.Arguments(0)
Set colMatches = objRE.Execute(strFileContents)
WScript.Echo vbNewLine & "Resulting Matches:"
For Each objMatch In colMatches
WScript.Echo "At position " & objMatch.FirstIndex & " matched " & objMatch.Value

3. Replace Function

regular expressions in vbscript examples/VBScript RegExp Object

Dim myPattern,myString 
Set myPattern=new RegExp
myPattern.pattern="[2,3,4,5,6,7]
myString="I have 2 glasses with 3 tablespoon" 
msgbox myPattern.Replace (myString,"many")

4. How to match a digit characters with regular expression?

regular expressions in vbscript examples/VBScript RegExp Object

Dim myReg,myString
Set myReg=New RegExp
myReg.Pattern="[0123456789]" //this is for specific
myReg.Pattern="[0-9]" //this is for generic
myReg.Pattern="/d" //this is for generic
myString="There are 5 persons in the car"
msgbox myReg.Replace(myString,"Many")

O/P- There are many persons in the car.

5.How not to match a digit in regular expression?

A caret (^) or a circumflex sign does the trick here.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

2 COMMENTS

  1. It’s actually a cool and useful piece of info. I’m glad that you just shared this useful information with us. Please stay us up to date like this. Thanks for sharing.

  2. Good day! I simply wish to give you a huge thumbs up for your excellent info
    you’ve got here on this post. I will be coming back to your web site for more soon.

LEAVE A REPLY

Please enter your comment!
Please enter your name here