A regular expression is a string that describes or matches a set of strings. It is often called a pattern as it describes a set of strings. In this article we are going to discuss about using Regular expressions in VB Script / UFT One (formerly QTP).
For those who are beginners to programming, you should note that regular expressions are not limited to VB Script alone. These are used widely used across programming and scripting languages and are known to be very powerful for sifting through huge amount of data or for pattern matching. Knowledge of regular expressions may be helpful not only for UFT One / QTP projects but also in day-to-day life spent on a computer.
Here is an excellent comic by XKCD which depicts the power of regular expressions.
What are regular expressions? An example?
Let us dissect our definition above and answer this question with help of a simple example in context of UFT.
When working on particular applications let’s say one of the property values called id
is dynamic, that is, on every run you notice that the value of id
property is getting changed. So when you recorded your script, the value of id
property was REUFT101
. When you replayed the same script, value of id
changed to REUFT120
, on next iteration it changed to REUFT105
. You can easily identify a pattern here whereby the last two digits of id
value are getting changed.
Can we write a single string that can match ALL these dynamic values? Yes, and the answer lies with regular expressions. You can write REUFT1\d\d
The first five characters were fixed so we need not regularize them, however the last two characters were dynamic digits. We need to ensure that our regular expression should be able to match any single digit between 0 to 9. The regular expression for that is \d
. Since we need to match two digits we have used it twice \d\d
Where can we use them in QTP/VB Script?
This was a simple example of what can be done with regular expressions. In QTP/VB Script, we can use regular expressions to deal with dynamic property values in object repository OR in descriptive programming OR while creating checkpoints with varying values.
Handling Dynamic Property Values in Object Repository
Let us say you record an object and find that one of the properties of that object (as circled below) is dynamic whereby the last digit can change to any single digit or letter on subsequent runs.
UFT (QTP) provides an easy way to change the last digit to a regular expression. We will use the regular expression .*
to match any character zero or more times. Check the illustration below
Handling Dynamic Property Values in Descriptive Programming
When using inline descriptive programming technique, the same example above can be written as
Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("type:=text","name:=userName.*","html tag:=INPUT").Set "mercury"
Handling Dynamic Properties Values in Checkpoints
In the example statement shown in the illustration below, once you have inserted a checkpoint, right click on Checkpoint(“username”) and go to Checkpoint properties. Highlight the property value which you wish to make a regular expression.
What are some of the commonly used regular expression characters?
Given underneath is one of the most widely used and ever confused backslash character. The remaining expressions are serialized below that.
Using the backward slash Character
A backward slash \
instructs UFT / QTP to treat the next character as a literal character, if it is otherwise a special character. The backward slash \
can also instruct QTP to recognize certain ordinary characters as special characters.
As seen in the example above, dot .
is used as a special character. There may be instances where you may want to use a period as a literal character otherwise it may be mistaken as an indication for regular expression .
(example: www.hp.com) In such a case you can use backward slash to escape the special meaning of this period. (example: www\.hp\.com
)
Note: If a backward slash character is used before a character that has no special meaning, the backward slash is ignored.
Expressions & Explanation
Match any single character .
Example: lear.
will match learn
, lear1
or lear%
Match the preceding character zero or more times *
Example: zo*
matches either z
or zoo
.
Match any string starting with text specified just before the pattern .*
This is one of the most frequently used regular expressions pattern. Try to understand it properly.
Example: learn.*
will match any string starting with learn
. Let us say you encounter a string “We are on learnqtp” but you see that the characters after the string “learn” keeps changing. In such a case you can simple make a regular expression as “We are on learn.*”
Match the beginning of a line ^
Example: ^learnqtp
will match any line that starts with learnqtp
hence it will match learnqtp
, learnqtp.com
but not www.learnqtp.com
Match the end of a line $
Example: learnqtp$
will match any line that ends with learnqtp
hence it will match learnqtp
, www.learnqtp
but not www.learnqtp.com
Match the preceding character one or more times +
Example: zo+
matches zoo
but not z
.
Match the preceding character zero or one time ?
Example: a?ve?
matches the ve
in never
.
We have touched upon the ones that are used more frequently. For a complete and exhaustive list check Microsoft’s MSDN
How to validate Regular expressions from inside QTP (UFT)?
With the launch of UFT 11.5, HP integrated a regular expression evaluator right inside UFT’s IDE. This reg ex evaluator is accessible through Tools > Regular Expression Evaluator
Some common examples of Regular expression
Example 1: Regular expression to match all valid email addresses
^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$
We have assumed above that the TLD (.com, .org, .net, .in) is between 2 to 4 characters long. Also, we have only taken the case for lower case characters for validation.
Example 2: Regular expression to match a date in MM/DD/YYYY format
^([1-9]|1[0-2])/([1-9]|[1-2][0-9]|3[0-1])/[0-9][0-9][0-9][0-9]$
Example 3: Regular expression to match your production, test or demo instances
Say you run a site, example.com which is configured for prod.example.com, test.example.com and demo.example.com. You need to make the first variable part as a regular expression. Here is how the regular expression would look like (given that the string example.com does not change)
(prod|test|demo)\.example\.com
This brings us to the end of our guide on Regular Expressions with UFT. Feel free to comment below if you have any further questions.
I need help. Can anyone do the regular expressions of the following :
Checking ***1819~9 $7,701.02
Here the amount is dynamic.
Thanks
hi iam facing issue with my script
my question is when i run the my script first time there is a one link the link property value is first time 7 digits and next is 8 digits so here the link property value changing dynamically so iam not able clicking the link in this so pleas help how to handle this situation and how to click the link with out fail my script.
Sir,
I have faced an issue. Suddenly my UFT could not find the objects where I have used regular expressions. Kindly give me a solution please. It was working fine. From yesterday onwards those objects are not identified in QTP.
Hi Ankur,
Are there enough openings on QTP(UFT) now when compared to Selenium.
@Both these tools have their own utility. No single tool can capture and fulfill needs of every organization. For long , UFT (QTP) has remained the #1 tool of choice for enterprises among commercial tools and it is still going strong.
how to write regular expression for this
[PO Request No : REQ3967PT-Select Part]
PO Request NO: REQ^([0-9]{4})([a-z]{2})$
Hi, Supposing i want to identify a text inside a innerhtml and select the entire inner html if the text exists, is it possible to add such a regex in OR to identify a element in a page.?
Hi,
I am trying to send page name dynamically.
Please tell me what’s wrong in the below code
Browser(“Browser”).Navigate linkURL
pageName = Browser(“Browser”).Page(“micclass:=Page”).GetROproperty( “title” )
Browser(“Browser”).Page(pageName).WebEdit(“Write something…”).set(msg)
Browser(“Browser”).Page(pageName).WebButton(“Post”).Click
TIA.
@Magulan:
There are multiple mistakes in this code:
1. Once DP is used, you cant go back to OR reference in the same statement.
2. If you are writing in this format, pageName that you are fetching should be already present in the Object repository. In your case it doesn’t make sense. Better go with DP.
Hi,
I’ve noticed from some of the images in your article that you seem to have the option to use parameters and regular expressions at the same time, is this the case? I’m currently trying to define an object where this method would make it a lot easier for me, however the option isn’t available to me, I can only use one or the other. Is there somewhere in QTP to enable this option? I’ve had a look around but couldn’t find anything. I’ve also tried editing the xml file but this worked only temporarily for me, only when highlighting in application from the repository but when it was used in the code it would not recognise the object.
Just trying it again now and the highlight in application no longer works either.
Ok I think I’ve got it working now actually, I’d still be interested to know if there’s any option to enable so that I don’t have to edit the xml file each time.
Thanks
Steve
Hi,
i need help on identifying object. in my case object is changing dynamically as below.
object name is “eligHistory.100119392.100084782.recordStatus” and sometimes for the same element id displays as “eligHistory.100116567.recordStatus”.
please help me to get the solution.
Thanks
@shivaraj: There can be lots of ways. It depends upon your needs. For example:
If you need to just have eligHistory as constant you can write eligHistory.*
If you need to have eligHistory.xxxxxxxx.recordStatus, you can write eligHistory\..*\.recordStatus
hi , in QTP i am trying to click on a link
Page(“SEARCH – concrete – Home”).Link(“products (155)).Click
In the object property i changed the properties name and text to “products \(\d\) ”
155 can be any number of digits
but this does not work getting warning object not found
Page(“SEARCH – concrete – Home”).Link(“products \(\d\)”).Click
please help
@azeem: Please ask your question here
I am having difficulty in getting a Regular Expression for $36.36 Can ayone please help.
hey, how to handle this scenario 123regular123
@unknown:
Please go to IE->Options->security
and below you will find a checkbox clicked. Please uncheck that.
Also first open IE and then QTP and try.
Hi Ankur
I am facing a problem while working for a certain scenario in my project
The application is such that a radio button needs to be checked every time in n corresponding screens(n can vary from 1 to 10 depending on requirement)
on recording and adding the spying the object we found that the unique prop as
For the first screen property is
(imag3-indraswe)
for the second screen property is
(imag3a-indraswe)
for the third screen property is
(imag3b-indraswe)
Can you say me how will i handle it through regular expression (here the only changes is 3,3a,3b and so on in diff screens)
i used(imag3.*-indraswe) but it gives ambiguity error
even i tried (imag3[a-z]-indraswe) but it dint work
plz help me
Regards
Swayam
Hi
Ankur
I had a problem
in my system webobjects are recognizing as winobjects why ??
am using windows7,QTP10.00& IE9
Hi Ankur,
The AUT’s web page has 100+ check boxes/edit boxes,with out adding all those to OR,how can i manage the script using all those…pls ans me
Thanks
venket
Hi Shreyya,
If you confirmly know the two possible values use [Morning|Evening] Shreyya
Thanks
hello Ankur,
I am attempting to use Regular expressions to identify a link which could have two possible values eg.,
Morning Shreya
Evening Shreya
I tried [a-z] but it doesnt identify the object with this reg expression.
plss help !! much appreciated
Hi,
Since the ‘M’ and ‘E’ of “Morning” and “Evening” are uppercase, you can try [A-Z]|[a-z]
Hi I have the following issue with regex
some status msgs on the are like
:- “Your PreClearance request has been DENIED. For details please check Transaction Id – 8886”
I wanted to regularize them inside the OR so as to take values as DENIED|APPORVED & any 4 digit as transaction id as in above status msg
I Tried the below i am not able to idetify it from OR :-
Your PreClearance request has been (DENIED| APPROVED). For details please check Transaction Id – [0-9999].*
need help !
Thanks
Rohit
Hi Ankur,
My name is Balaji. I have prepared an automation suit for a web based application using QTP. Recently I have encountered a problem with the versioning of the browser and page properties which is introduced recently into application. Previously suppose brower name is like “Welcome Customer” and now it is “Welcome Customer 1.2.25” some thing like that and its keeps on changing. Could you please tell me how to handle this with regular expression? I just want to compare the static text “Welcome Customer” and the version part should be ignored. Thanks in advance.
hi Ankur,
My question in 1 of the interview was, how can u identify if the object is changing dynamically from edit box to textbox or any other thing. how can u find which object is present application?
Can u please help me
@deepika
You made some minor mistakes in your regular expression. First, if you want to match a character such as backslash (\), you have to use the backslash itself before the character because backslash is a special character in regular expressions, i.e. it has special meaning. So special characters such as period (.), asterisk (*), and plus sign (+) should be preceded by a backslash if you want to use the character literally. e.g. \. or \* or \+ (there are more special characters)
So you should say \\ to mean I want to match the literal backslash character.
Also, in your month, it should be [0-1][0-9] since the first digit of a month can only be a 0 or a 1. Your correct regexp would be:
[0-3][0-9]\\[0-1][0-9]\\20[0-9][0-9]
Hi ankur,
plz help in regular expression concept. there is some problem in rs. I have made rs for “date”. It is a Mercury tour website and i m making rs for date. i wrote the format of date like this “[0-3][0-9][0-9][0-2]20[0-9][0-9]” for ddmmyy.but when we run this it is not running properly.i have also checked rs check box.
Hi Ankur,
I need QTP to match a floating point number. My requirement is to extract data (basically percentage) which is displayed as a label on a page and then enter this value in a web edit. I could extract the data and now i want to compare it with a regular expression(since percentage values are not the same all the time). The range of this value is from 0-9999.99. How can i enter a regular expression for this? I used “if elems(i).object.innerText = “[0-9999.99]” then “. This doesnt work…
Hi Anand,
Try to use regexp ^ctl00_RadWinMgrMP[0-9]{1,}
Hope this may help you to resolve your issue.
Regards,
Suresh
psk_suresh@yahoo.com
Hi ANkur, thanks for the great article.appreciate your time in solving all newbie questions here.
I have a strange issue , im using qtp in recording a sharepoint based project, where we have a button(link), on click of that a Lightbox window pops up, with different textboxes and calendar control, after recording with QTP, whenever i try to playback, the Lightbox is not getting recognized, the name is something like “ctl00_RadWinMgrMP1268239458212” , in that the Number part keeps generating randomly, can you pleas help me how to use regular expression in ths scenario.
appreciate your great help.
thanks
Anand
To Ramakrishna,
Did u try Low Level Recording for that object?
Hi Ankur it’s my general doubt not specific to RegExp…if Object spy also could not identify the object properties , means object could not be identified then wat approach we follow to identify tht object? as here Des prog also could not work? is it need to go for obj identification(smart idtn) or anything else approach is there?
hi,
i test an application where one of the field gives a text in this format
“ABC already exists.”
In my case ABC is a dynamic value and might take any three characters. i tried using the below mentioned formats to identify the object, however it did not seem to work.
*.already exists.
??? already exists.
Please help
Reva,
Regular expressions refer to data lexically – so to find if text represents a number between 100 and 632, one could do this:
([1-5][0-9][0-9]) | (6[0-2][0-9]) | (63[0-2])
which means:
100 to 599 OR 600 to 629 OR 630 to 632
What this means:
This breaks up our 3-digit number lexically. One of three cases will be the number:
1. A number between 100 and 599 will include a number from 1 to 5, a number from 0-9, and a number from 0-9
2. If the number starts with a 6, the second digit can only be from 0 to 3. If the second digit is between 0-2, the third digit can be 0-9. The second option accounts for 600 to 629
3. If the first digit is 6, and the second digit is 3, we should limit the range of the third digit to 0-2. The third option accounts for 630 to 632.
I hope that helps.
Steven
Hi Ankur,
In my application there are three drop down each for Date, Month, and Year.Whern we select a particular date and go to the next page the selected date appear as Text element.( mm/dd/yyyy)
I want to first concatenate date from three drop down box and verify it with the next page date which is appearing in mm/dd/yyyy format.
Also if i parameterize the date same shold be work for each iteration.
Can you please help me
Hi Ankur,
I had a question here.
How do I go to a particular line of the code from other part of the code without any error?
Actually, we use on error resume next when an error occurs. But without any error How do we jump from one line to other part of code in the same script?
please write down a small script as an example for understanding about how to use regular expression.
I need to get an example for how to write a regular expression for any simple object in “Flight Reservation” Application.hope anyone will guide me with a proper example as soon as possible.
Hi,
i have included reguler expression but its not working
could anyone please spacify how can validate my email address
Chetan
Hai all
I want some information regarding webtable,i want to check a web table cell’s font size please help me to do this i did’t find any functions in webtable object and vbscript.
hi
i want to use regular expression to find no is in the range [100-632]could you please help
thanks
reva raghavan
Hi All,
I have stopped taking questions on this blog. Please use Testing Tools forum for posting QTP questions.
HI Ankur,
i want to use Regular expression on sieble application. we have different environments like Development, Systems, Training, Testing etc… of the same application
Now my problem, I have to prepare a basic script and I need to modify the script and Object repository to work on all the environments.
Here my difficulty is:
Browser(“Sieble- Systems Application”). Page(“Sieble- Systems Application”).Frame(“Sieble – Systems Application”).Button ( “New”).Click
For Development environment
Sieble- Development Application
For Training
Sieble- Training Application
For Testing
Sieble- Testing Application
Looks like that can u suggest me how to use Regular expression in this case and similarly what type of modification I need to do in the Object repository also.
“Systems Application” à This part I need to parameterize for different environments.
Looking forward for your Help in this regard.
Hi,
Regular expression won’t work in script editing. you need to modify the OR with the regular expression.
Regards,
Sreenivas
Hi Ankur ,
On my webpage there is a webelement having text “Pre-Authorize Basic Loan Information(Write Access) – Exec Time: 497 ms -“
The last part of the string (497 ms -“)changes always , say “540 ms -” , “650 ms -” depending on exec time . Rest of the string remains same. I want to write code :
If Browser().Page().Frame().Webelement(“text:=Authorize Basic Loan Information(Write Access) – Exec Time:****
“).Exist(10) Then
msgbox “Pass”
Else
msgbox “Fail”
End If
Please guide me how I can use regular expression here for the last part of string .
Hi Ankur,
Can you please tell me how to use regular expression to verify numbers (non-zero). I tried “[1-9].*” but it didnt work. I was to ensure its a valid number of any digits and not zero.
Thank you Vikas, It worked.
This was very quick……… thank you once again
@ lingam: You can post your questions here itself.
@ Vikas: Thanks for answering the queries here. I need help from people like you to keep this blog alive. Going with the volume of question being asked here daily and my work time lines, at times it becomes difficult for me to keep track.
Hi Ankur
This is kiran. i am working as test engineer in vission apps.I am appriciating u r work. this is very useful to all who are coming to testing field. I want u r id. plz send to kiranreddy.lingam@gmail.com
buy ankur
ur brother
kiran
Hi swapna,
I dont think regular expressions work in the place where you have used it.
What i meant was
it cant be used in the statement
‘SwfWindow(“frmMain”).SwfWindow(“Lead Profile”).SwfLabel(“[0-9][0-9][0-9]-[0-9][0-9]”).Click
Instead use it in the “Name” property of the object “SwfLabel” in Object repository.
change the “Name” in OR as
[0-9]{3}\-[0-9]{1,}
which means , it will recognise all those labels which have a name
consisiting of
3 digit number
followed by a “-“
followed by a number which has 1 digit or more
Hi Raji
Use this expression
Fax Order No. [0-9]{1,}
Now it will match all the fax order numbers which has 1 or more digits.
Hi,
I am bit confused on reg exp.
My code is ‘SwfWindow(“frmMain”).SwfWindow(“Lead Profile”).SwfLabel(“008-16”). Click 80,5 and I replaced with the reg exp to
‘SwfWindow(“frmMain”).SwfWindow(“Lead Profile”).SwfLabel(“[0-9][0-9][0-9]-[0-9][0-9]”).Click 80,5
I get the error “The “[0-9][0-9][0-9]-[0-9][0-9]” SwfLabel object was not found in the Object Repository.
Check the Object Repository to confirm that the object exists or to find the correct name for the object.”
The object exists in OR with object name “008-16” and class swfLabel.
How do I over come this issue?
It did not work for me.throw an error.
//Cannot find the “Fax Order No. 84” object’s parent “Flight Reservation” (class Window). Verify that parent properties match an object currently displayed in your application.//
can you tell me the meaning of this eeror.I inserted a std check point for the dialogue box caption “Fax Order No. “.Is this the correct way to do this?
@ Raji,
There are many ways to solve the above problem.
You can use
i)Fax Order No. [0-9][0-9][0-9]
OR
ii)Fax Order No. [0-9]{3} //if the digits are exactly equal to 3//
OR
iii) Fax Order No. [0-9]{3,} //if the digits are equal to or greater than 3
Hi Ankur,
Can you tell me how to write regular expression for a string “Fax Order No. 211” where the number 211 changes dynamically after each run.
In this case the check point inserted should be output valu or standard check point?
hi ankur,
how to use regular expression to write coding for version controlling.
hi ankur
thanks 4 ur help
due to that i am successful in doing reg exp
@ Chitra
The concept of regular expression is same everywhere be it win runner/ QTP or what that matter any scripting or programming language.
You can refer the above post to get yourself familiarized with regex.
hi Ankur
can u plz tell me the concept of Regular expression here in QTP,as we use it for variying Window lables in Winrunner,then In QTP we use it for same purpose
plz reply me how to do it and why
Hi Swanandk,
Yes you can use regular expression in recovery scenarios. For your specific case try to use \w* after your application name.(Dont forget to check the Regular expression check-box…while defining RS for pop-up window title name) This will match any word character 0 or more times.
Let me know if it solves your problem
Hi Ankur,
Can we use regular expression in recovery scenarios ?
I have a condition where a pop-up window appears randomly, but the window name has application name followed by some error message (say, “ABC – No data found”).
I want to use regular expression in recovery scenario saying if pop-up window appears having ABC in the window name, then click “OK” & proceed further.
I tried a lot, but was unable to do it. Is there something that i am missing ?