Tuesday 19 November 2013

Applications and Examples of Regular Expressions in QTP

Well - Before I jump to the applications of regular expressions, Let me explain what regular expressions are.

Definition :
A regular expression is a pattern of characters(meta characters and special characters).

General Applications of Regular Expressions :
Where are regular expressions used.
  1. Pattern Matching in Strings
  2. To find the occurrences of one string/pattern in given string.
  3. To replace the patterns with another string in a given text
All programming languages support the use of regular expressions. 

Regular Expressions in QTP :
We can use regular expressions in qtp in many ways as mentioned below.

  1. In QTP, object repository is used to store the object's properties and values. We can use regular expressions to specify the object property value.This helps us using same test object to identify multiple run time objects in the application. 
  2. We can also use regular expressions in descriptive programming. 


QTP examples on Regular Expressions:  
As I said earlier We use regular expressions to check if the given string matches the specified pattern.
For example - Consider a scenario where you have to validate that the given string should be a valid email address.

So list of valid email addresses are - reply2sagar@gmail.com, ayx@jjjj.in etc
Some of the invalid email addresses are - kjkjj@fdff, @dfdf.in etc

With the help of regular expression, you can easily validate the email address.

Syntax of Regular Expression in QTP:
In any QTP program related to regular expression, you will follow below steps.
  1. Create a regular expression object (RegExp)
  2. Define the pattern using RegExp object's pattern property.
  3. Use test method to check whether the given string matches with the pattern specified in step 2.
'Create the regular expression object
Set myRegEx = New RegExp 

'Specify the pattern (Regular Expression)
myRegEx.Pattern = "[a-z0-9]+@[a-z]+\.[a-z]+"

'Specify whether the matching is to be done with case sensitivity on or off.
myRegEx.IgnoreCase = True

'Use Test method to see if the given string is matching with the pattern
isMatched = myRegEx.Test("reply2sagar@gmail.com")

Variable isMatched will be true if the string "reply2sagar@gmail.com" matches with the given pattern 
"[a-z0-9]+@[a-z]+\.[a-z]+"

No comments:

Post a Comment

Please Leave your reply. We value your feedback and inputs

Best QTP Books

Everything About QTP

Hello Friends,
You can find QTP study material, Multiple choice questions (mcq), QTP question bank, QTP question papers, QTP notes, QTP questionnaire, scenario based QTP interview questions, QTP tutorial and QTP training on this site.

If you are a fresher or experienced QTP professional with (1/2/3/4) years of experience, this blog is just for you.