A regular expression is a string that describes or matches a set of strings. It is often called a pattern as it describes set of strings. In this article we are going to discuss about using Regular expressions in VB Script/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 QTP projects but also in day-to-day online life.
Here is an excellent comic by XKCD which depicts the power of regular expressions.
Let us dissect our definition above and answer this question with help of a simple example.
When working on a 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 in 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.
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.
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.
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 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
With the launch of UFT 11.5, HP has integrated a regular expression evaluator right inside UFT’s IDE. It is accessible through Tools > Regular Expression Evaluator
Example 1: Regular expression to match all valid email addresses
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
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)
If you want to keep track of further articles on UFT (QTP). I recommend you to subscribe by Email and have new UFT articles sent directly to your inbox.