Contents
- 1 Regular Expressions in QTP/UFT/VBScript:
- 1.1 What is Regular Expression?
- 1.2 Regular Expressions in QTP/UFT:
- 1.3 RegExp object
- 1.4 1) Match File Names in a Directory against Regular Expression
- 1.5 2) Match Content in a File against a Regular Expression
- 1.6 3. Replace Function
- 1.7 4. How to match a digit characters with regular expression?
- 1.8 5.How not to match a digit in regular expression?
- 1.9 Remembered matches(regular expressions in vbscript)
- 1.10 Back referencing
- 1.11 Test Method
- 1.12 The Matches Collection
- 1.13 6.String Manipulation using Regular Expression
- 1.14 Regular Expression cheat seat:
- 1.15 Ways of Regular Expressions:regular expressions in vbscript
- 1.15.1 Backslash Character:
- 1.15.2 Matching Any Single Character:
- 1.15.3 Matching Any Single Character in a List:
- 1.15.4 Matching Any Single Character Not in a List:
- 1.15.5 Matching Any Single Character within a Range:
- 1.15.6 Matching Zero or More Specific Characters:
- 1.15.7 Matching One or More Specific Characters:
- 1.15.8 Matching Zero or One Specific Character:
- 1.15.9 Grouping Regular Expressions:
- 1.15.10 Matching One of Several Regular Expressions:
- 1.15.11 Matching the Beginning of a Line:
- 1.15.12 Matching the End of a Line:
- 1.15.13 Matching Any AlphaNumeric Character Including the Underscore:
- 1.15.14 Matching Any Non-AlphaNumeric Character:
- 1.15.15 Combining Regular Expression Operators:
- 1.16 Share and Enjoy !
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?
- We need to create a new regular expression object
set myVar=New RegExp
- We need provide the key that needs to be matched.
myVar.pattern="[[mySearchText]]"
- myLine- “the line or string from where the matching will happen”
- 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.
“[^,0-9]”=> will not match any digit in the range 0-9
“[^\d]”=> does the same
“[\D]”=> does the same
How to create a pattern or range in regular expression?
While the above example like”[ 0 1 2 3 4 5 6 7 8 9]”or “[0-9]” or “/d” provides a generic pattern.We can create our own pattern by providing range or repeat count .
- “\d” implies \d{1} that is single digit
- “\d{2}” implies double digit
- “\d{3}” implies three digit
How to specify minimum number or range using regular expression?
- “\d+”=>implies that one or more digits equivalent to\d{1,}
- “\d*”=>implies that zero or more digits equivalent to \d{0,}
- “\d?”=>implies that zero or one equivalent to \d{0,1}
we need to be careful using the last two.In order to change all the digits in the given string we need to Set global property as true.
Remembered matches(regular expressions in vbscript)
This technique is useful when one or all of the text matches, with our pattern while finding or replacing text.
regular expressions in vbscript examples/VBScript RegExp Object
Dim myReg,myString,myCol,matchFound Set myReg=New RegExp myReg.Global=True myReg.Pattern="http://(\w+[\w-]*\w+\.)*\w+" myString="http://www.google.com" myString=myString&VBcrlf & "http://www.bing.com" myString=myString&VBcrlf & "http://www.altavista.com" Set myCol=myReg.Execute(myString) For Each matchFound in myCol msgbox "Found valid URL" & matchFound.value Next
The URL pattern starts with “http://”always. Hence we need to hardcode it. Now the remaining pattern starts with \w that matches with [a-z A-Z 0-9]i.e then alphanumeric characters .
The second component is [\W-] that matches with any alphanumeric characters with a dash so if the URL is having a dash in between ,the code will be able to determine .
*is used for repetition.
Execute Method
Execute method searches a specific string and return matches as collection.
Syntax-
object.Execute(stringPattern)
Example
Dim myReg,myString,myCol,matchFound Set myReg=New RegExp myReg.Global=True myReg.Pattern="http://(\w+[\w-]*\w+\.)*\w+" myString="http://www.google.com" myString=myString&VBcrlf & "http://www.bing.com" myString=myString&VBcrlf & "http://www.altavista.com" Set myCol=myReg.Execute(myString) For Each matchFound in myCol msgbox "Found valid URL" & matchFound.value Next
If the Execute method does not have matched content, in that case also ,execute return a collection.It will be an empty collection.
Replace Method
Replace method searches and replaces a string specified with the search string .
Syntax-
object replace (String to search,string to replace)
Back referencing
Back referencing is a technique that allows us to stare some part of the string in the buffer(temporary). It is done with the help of a parentheses.We can override the saved part of the string with other regular expressions(“?:”,”?=”,”?!”)
Example
Dim MyReg,Mystring Set MyReg=New RegExp MyReg.Pattern="(\s+)\s+(\s+)\s+(\s+)\s+(\s+)\s+(\s+)" MyString=This is not so cool” Msgbox MyReg.Replace(s,$1,$2,$4,$5)
Op->This is so cool
Test Method
Test method searches a string by executing a regular expression.It returns Boolean –true to indicate the pattern is found and false –to indicate that the pattern is not found.
Syntax –
object.test (searchString)
Example
Dim MyRag,MyString Set MyReg=New RegExp MyReg.IgnoreCase=TRUE MyReg.Pattern="http://(\W+[\W-]*\W+\-)*\W+" MyString="My favourite search engine http://www.google.com" If MyReg.Test(MyString)then Msgbox "the string is having an URL" Else Msgbox "The string does not have a valid URL" End If
O/P ->The string is having an URL
The Matches Collection
This matches collection is a placeholders for match objects.Execute method returns this collection.If execute method is executed we get zero or more match objects in the collection.The below properties can be extracted from match object
- The string
- The length of the string
- The index value where the match is found
Note
We need to enable Global property to true to match in the whole string.Match support two properties
- Count-It returns the number of items present in the collection.
- Item-It returns the exact item based or the query on the list with a key as Index .
Match object periods three read only properties-
- First Index
- Length
- Value
The details are as follows
- First Index-returns the position where the match happens with a giving string.
Syntax-Object.FirstIndex
- Length-returns the length of a match in the search string.
Syntax-Object.Length
- Value- returns the value or text or string of a match found in a search string.
Syntax-Object.value
How to validate a phone number?(XXX)XXXX-(XXX)
Dim myReg,MyNumber Set myReg=New RegExp MyReg.pattern='\([0-9]{3}\[0-9]{4}-(0-9){3} MyReg.Global=True MyReg.Ignorecase=True MyNumber=Inputbox("Enter your mobile in (xxx)xxxx-xxx format") If MyReg.test(MyNumber)then msgbox "It is a valid mobile number" Else msgbox "It is not a valid mobile number" End if
How to find the white space?
The Syntax –”^[\t]*$”
- ^ matches the start of the line.
- [\t] matches tab character for finding space or more space characters.
- $ Matches end of the line.
Dim MyReg,MyString,objMatch,spaceMatch,sMsg Set MyReg=New RegExp MyReg.Global=True MyReg.pattern="^[\t]*$" Mystring="I have a big cat" Set spaceMatch=MyReg.Execute(Mystring) SMsg="" For Each objMatch in spaceMatch sMsg="Space found at position" objMatch.FirstIndexNumber msgbox sMsg Next
6.String Manipulation using Regular Expression
Making Global property True
specific match or using \b switch
Dim reg, strString Set reg=New RegExp reg.Pattern="/b of" //(/b matches the exact word) reg.Global=True strString="To take of is not easy of the ground. It needs a great offline practice." msgbox reg.Replace(strString,"off")
As the Global property is true , it matches with all words in the string.
Making Global property False
Dim reg, strString Set reg=New RegExp reg.Pattern="/b of" //(/b matches the exact word) reg.Global=False strString="To take of is not easy of the ground. It needs a great off line practice." msgbox reg.Replace(strString,"off")
O/P—>to take off is not easy of the ground.It needs a great off line practice. Only changes the first occurrence.
Regular Expression cheat seat:
The below patterns can be used with VBScript RegExp Object.
Character | Description |
---|---|
Indicates that the next character is either special character or literal | |
^ | Indicates a match at the beginning of the input |
$ | Indicates a match at the end of the input |
* | Indicates a match before the indicated character.Matches will be performed either zero or more times. |
+ | Indicates a match before the indicated character.Matches will be performed either one or more times. |
? | Indicates a match before the indicated character.Matches will be performed either zero or one time.It can also matches any single character except a newline character. |
(pattern) | Indicates a match with the given pattern.It returns a collection of items.To retrieve the pattern info we can use item[0],item[1],….item[n] |
(?:pattern) | This is a non Capturing search hence the match is not captures for future use.It is useful for putting OR condition |
(?=pattern) | This is a non Capturing search hence the match is not captures for future use.However it is a positive look head that matches the search string at any given point of time. Test(?1,2,3,8,9) matches Test in TestCase1,TestCase2…TestCase9 but not in TestData |
(?!pattern) | This is a non Capturing search hence the match is not captures for future use.However it is a negative look head that matches the search string at any given point of time. Test(?!1,2,3,8,9) matches Test in TestData but not in TestCase1,TestCase2…TestCase9 |
x|y | Indicates a match with a match will happen with either x or y |
{n} | Indicates a match will happen exactly n times,where n is greater than zero. |
{n,} | Indicates a match will happen at least n times where n is greater than zero and it must terminate with a comma. |
{n,m} | Indicates a match will happen at least n times and at most m times where n and m are greater than zero and m is also greater than n. |
[xyz] | Indicates a match with any character from the given character set. |
[^xyz] | Indicates a match with any other character which are not part of the given character set. |
[a-z] [A-Z] | Indicates a match with any character in the given range. |
[^m-z] | Indicates a match with a negative range of character. |
b | Indicates a match with a boundary for word.It identifies the positioning between a word and a space. |
S | Indicates a match with all non white space just opposite to /s |
B | Indicates a match with a non word Boundary. |
d | Indicates a match with a digit character. |
D | Indicates a match with a non digit character. |
f | Indicates a match with a form feed character. |
n | Indicates a match with a new line character |
r | Indicates a match with a carriage return character |
s | Indicates a match with any white space character including space,tab etc. It is equivalent to [fnrtv] |
w | Indicates a match with the word character including underscore [A-Za-z0-9_] |
t | Indicates a match with a tab character |
W | Indicates a match with the non word character like [^A-Za-z0-9_] |
. | Indicates a match with the dot |
| | Indicates a match with a Pipe |
{ | Indicates a match with the curly bracket start”{“ |
} | Indicates a match with the curly bracket end “}” |
Indicates a match with backslash () | |
[ | Indicates a match with the third bracket start “[“ |
] | Indicates a match with the third bracket end “]” |
( | Indicates a match with the first bracket start “(“ |
) | Indicates a match with the first bracket end “)” |
$num | Indicates a match with a positive number. |
v | Indicates a match with a vertical tab character |
n | Indicates a match with an Octal value may be 1,2,3 digits |
uxxxx | Indicates a match with an ASCII character represented by Unicode-XXXX |
xn | Indicates a match with n,where n is a hexadecimal value of 2 digits |
[m-z] | Indicates a match with a character not in the given range. |
Ways of Regular Expressions:regular expressions in vbscript
Backslash Character:
A backslash () can serve two purposes. It can be used in conjunction with a special character to indicate that the next character be treated as a literal character.
Alternatively, if the backslash () is used in conjunction with some characters that would otherwise be treated as literal characters, such as the letters n, t, w, or d, the combination indicates a special character.
Matching Any Single Character:
A period (.) instructs QTP to search for any single character (except for n).
Ex:
welcome.
Matches welcomes, welcomed, or welcome followed by a space or any other single character.
Matching Any Single Character in a List:
Square brackets instruct QTP to search for any single character within a list of characters.
Ex:
To search for the date 1867, 1868, or 1869, enter:
186[789]
Matching Any Single Character Not in a List:
When a caret (^) is the first character inside square brackets, it instructs QTP to match any character in the list except for the ones specified in the string.
Example:
[^ab]
Matches any character except a or b.
Matching Any Single Character within a Range:
To match a single character within a range, we can use square brackets ([ ]) with the hyphen (-) character.
Example:
For matching any year in the 2010s, enter:
201[0-9]
Matching Zero or More Specific Characters:
An asterisk (*) instructs QTP to match zero or more occurrences of the preceding character.
For example:
ca*r
Matches car, caaaaaar, and cr
Matching One or More Specific Characters:
A plus sign (+) instructs QTP to match one or more occurrences of the preceding character.
For example:
ca+r
Matches car and caaaaaar, but not cr.
Matching Zero or One Specific Character:
A question mark (?) instructs QTP to match zero or one occurrences of the preceding character.
For example:
ca?r
Matches car and cr, but nothing else.
Grouping Regular Expressions:
Parentheses (()) instruct QTP to treat the contained sequence as a unit, just as in mathematics and programming languages. Using groups is especially useful for delimiting the argument(s) to an alternation operator ( | ) or a repetition operator ( * , + , ? , { } ).
Matching One of Several Regular Expressions:
A vertical line (|) instructs QTP to match one of a choice of expressions.
Matching the Beginning of a Line:
A caret (^) instructs QTP to match the expression only at the start of a line, or after a newline character.
Matching the End of a Line:
A dollar sign ($) instructs QTP to match the expression only at the end of a line, or before a newline character.
Matching Any AlphaNumeric Character Including the Underscore:
w instructs QTP to match any alphanumeric character and the underscore (A-Z, a-z, 0-9, _).
Matching Any Non-AlphaNumeric Character:
W instructs QTP to match any character other than alphanumeric characters and underscores.
Combining Regular Expression Operators:
We can combine regular expression operators in a single expression to achieve the exact search criteria we need.
For example,
start.*
Matches start, started, starting, starter, and so forth.
we can use a combination of brackets and an asterisk to limit the search to a combination of non-numeric characters.
For example:
[a-zA-Z]*
To match any number between 0 and 1200, we need to match numbers with 1 digit, 2 digits, 3 digits, or 4 digits between 1000-1200.
The regular expression below matches any number between 0 and 1200.
([0-9]?[0-9]?[0-9]|1[01][0-9][0-9]|1200).
This is my first time go to see at here and i am in fact pleassant
to read all at single place.
I think this is a real great article post.Much thanks again. Keep writing.
I was able to find good info from your articles.
Thanks for the diverse tips shared on this blog. I have realized that many insurance agencies offer customers generous savings if they opt to insure several cars with them. A significant amount of households have several autos these days, in particular those with old teenage young children still living at home, as well as savings in policies can certainly soon increase. So it is good to look for a great deal.
Thanks , I’ve recently been looking for info about this topic for a while and yours is the best I’ve came upon so far. However, what concerning the conclusion? Are you certain in regards to the supply?
Hmm is anyone else having problems with the
pictures on this blog loading? I’m trying to find out if its a problem on my end or if it’s the blog.
Any suggestions would be greatly appreciated.
Hi! I’m at work browsing your blog from my new apple iphone! Just wanted to say I love reading your blog and look forward to all your posts! Carry on the great work!
I am also commenting to let you understand of the really good experience our girl obtained reading through your web site. She mastered several details, which included how it is like to possess an awesome teaching nature to get other people very easily know selected grueling matters. You truly exceeded her desires. Thanks for presenting those necessary, trusted, revealing and cool tips on your topic to Janet.
Great blog here! Also your internet site loads up extremely fast!
What web host are you currently using? May I get
your affiliate backlink to your host? I wish my site
loaded as fast as yours lol
This design is steller! You certainly know how to have a reader amused.
Between your wit along with your videos, I was almost moved to start my own blog
(well, almost…HaHa!) Excellent job. I really enjoyed what you needed
to say, and over that, the way you presented it.
Too cool!
I am certain this article has touched all of the
internet people, its really really nice post on strengthening new blog.
Wow! This can be one particular of the most helpful blogs We’ve ever arrive across on this subject. Actually Fantastic. I’m also an expert in this topic so I can understand your hard work.
Greetings! I know this is kinda off topic however I’d figured I’d ask. Would you be interested in trading links or maybe guest writing a blog post or vice-versa? My site covers a lot of the same subjects as yours and I think we could greatly benefit from each other. If you happen to be interested feel free to shoot me an email. I look forward to hearing from you! Awesome blog by the way!
Good blog post. Things i would like to bring about is that pc memory must be purchased if your computer can’t cope with what you do along with it. One can deploy two random access memory boards having 1GB each, as an example, but not one of 1GB and one having 2GB. One should look for the company’s documentation for one’s PC to ensure what type of memory space is necessary.
Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You clearly know what youre talking about, why waste your intelligence on just posting videos to your weblog when you could be giving us something enlightening to read?
Appreciate this post. Will try it out.
This actually answered my drawback, thank you!
Way cool! Some extremely valid points! I appreciate you penning this post plusthe rest of the site is also really good.
Hi there! I could possibly have sworn I’ve been to
this site before but after checking through a number of the post I realized
it’s unfamiliar with me. Anyhow, I’m definitely glad I discovered it and I’ll be book-marking
and checking back frequently!
Yet another issue is that video games usually are serious naturally with the major focus on mastering rather than leisure. Although, we have an entertainment part to keep children engaged, every game is often designed to work on a specific expertise or area, such as mathmatical or research. Thanks for your article.