Regular Expressions in VBScript or QTP or UFT

23
1700
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

23 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.

  3. Hi there it’s me, I am also visiting this web site daily,
    this site is in fact pleasant and the users are truly sharing pleasant thoughts.

  4. Wonderful items from you, man. I have understand your stuff
    previous to and you are simply too fantastic. I really like what you have bought right here, really
    like what you’re stating and the way during which you assert it.
    You’re making it enjoyable and you continue to take care of to stay it smart.
    I cant wait to learn far more from you. This is really a wonderful
    website.

  5. Hey! Someone in my Facebook group shared this
    website with us so I came to look it over. I’m definitely enjoying
    the information. I’m bookmarking and will be tweeting this to my
    followers! Excellent blog and great design and style.

  6. Oh my goodness! Incredible article dude! Thank you so much, However I am experiencing issues with your RSS.

    I don’t know the reason why I can’t join it. Is there anybody else getting similar RSS problems?
    Anybody who knows the solution can you kindly respond?
    Thanks!!

  7. Wow, wonderful weblog layout! How lengthy have you
    ever been blogging for? you make running a blog look easy.
    The full look of your website is great, as smartly as the content material!

  8. Have you ever thought about publishing an ebook or guest authoring on other websites?
    I have a blog based on the same information you discuss and would really like to have you
    share some stories/information. I know my viewers would appreciate your work.

    If you’re even remotely interested, feel free to shoot
    me an email.

  9. I truly love your website.. Great colors & theme. Did you develop this web site yourself? Please reply back as I’m attempting to create my own website and would like to know where you got this from or what the theme is called. Many thanks!

  10. Someone essentially lend a hand to make significantly
    articles I’d state. This is the very first time I
    frequented your website page and to this point? I amazed with the analysis you made
    to make this particular post amazing. Fantastic activity!

  11. Hello! I just want to supply a massive thumbs up with the excellent information you have here for this post. I’ll be coming back to your blog site for more soon.

  12. I precisely needed to say thanks once more. I am not sure the things that I might have tried without these suggestions contributed by you regarding that industry. It has been an absolute difficult issue for me personally, nevertheless finding out a specialized way you handled the issue forced me to leap with contentment. I’m just thankful for this work and hope that you find out what an amazing job your are putting in training people by way of your web blog. More than likely you have never met all of us.

  13. Great write-up, I am regular visitor of one’s blog, maintain up the nice operate, and It’s going to be a regular visitor for a long time.

  14. Attractive section of content. I just stumbled upon your weblog and in accession capital to assert that I get in fact
    enjoyed account your blog posts. Anyway I’ll be subscribing to your feeds and even I
    achievement you access consistently rapidly.

  15. I really wanted to write a small word to express gratitude to you for all the fabulous instructions you are placing here. My rather long internet lookup has now been honored with useful knowledge to share with my good friends. I would believe that most of us readers are very blessed to exist in a remarkable network with very many special professionals with valuable pointers. I feel truly privileged to have used your webpages and look forward to really more entertaining minutes reading here. Thanks a lot again for a lot of things.

  16. Thanks a lot for providing individuals with remarkably wonderful opportunity to discover important secrets from this site. It is always so nice and as well , jam-packed with a good time for me and my office co-workers to search your website really 3 times a week to learn the fresh items you will have. And of course, I’m usually pleased with all the surprising creative ideas you give. Some two facts in this post are without a doubt the most effective we have all had.

  17. I think the admin of this website is really working hard in support of his web page, because here every stuff is quality based stuff.

  18. I needed to send you this tiny observation to finally say thanks over again relating to the pretty knowledge you have featured in this case. It is particularly generous of you to make unreservedly what a few people could possibly have sold as an ebook to help with making some dough for themselves, most notably given that you might well have done it in the event you considered necessary. The basics in addition served like a good way to understand that someone else have the identical fervor like my very own to find out great deal more with reference to this condition. I know there are a lot more fun occasions in the future for people who read through your blog post.

  19. I am only commenting to make you understand of the impressive encounter my friend’s child gained reading through yuor web blog. She learned a lot of details, including what it’s like to possess an excellent teaching mood to make the others really easily fully understand specified impossible matters. You undoubtedly surpassed readers’ expected results. Thanks for rendering the helpful, safe, explanatory not to mention easy tips about that topic to Sandra.

LEAVE A REPLY

Please enter your comment!
Please enter your name here