Tuesday, 14 December 2010

QTP - Environment Variables

Leave a Comment
QTP & Environment Variables

Welcome
  QTP – VBScript (Part 1)
  QTP – VBScript (Part 2)
  QTP – VBScript (Part 3)
  QTP – VBScript (Part 4)
  QTP – VBScript (Part 5)
  QTP – VBScript (Part 6)
  QTP – VBScript (Part 7)
  QTP – VBScript (Part 8)
  QTP – VBScript Examples (Part9)
  QTP – VBScript Examples (Part10)
  QTP – Environment Variables
  QTP – Arrays
  QTP – Error Handling
  QTP – Functions
  QTP – Frameworks
  QTP – MORE….
Contact@
G A Reddy
QuickTest QTP
http://QuickTestQTP.BlogSpot.com
Environment variables
Environment variables represents the QTP Environment object, which enables you to set or retrieve the value of environment variables
Environment variables are like global variables
Environment variables can be accessed through out from any part of the script.
The values of these variables remains same irrespective of the number of iterations (unless you change them through scripting).
These variables can prove to be very useful when you want a variable to be shared across various reusable actions/tests or functions.
Environment Variable - Types
Built-in
Built-In: These are the internal variables that are provided by QTP.
These provide valuable information like the path of the folder where test is located, the path of the results folder, the name of the action iteration or the OS version.
User Defined.
These can be further defined into two types.
User defined Internal
These are the variables that we define within the test.
These variables are saved with the test and are accessible only within the test in which they were defined.
Example:
AppURL=Environment.value(“URL”)=http://newtours.demoaut.com
SystemUtil.Run AppURL
User defined External
These are the variables that we predefine in the active external environment variables file.
These can be created using a list of variable-value pairs in an external file in .xml format.
Example:
MyEnvFile = “C:/EnvVars.xml”
Environment.LoadFromFile MyEnvFile
Browser().Page().WebEdit(“userName”).set Environment.value(“Uname”)
Environment variables
  To set the value of a user-defined, environment variable:
  Environment (VariableName) = NewValue
  To retrieve the value of a loaded environment variable:
  CurrValue = Environment (VariableName)
  Example:
  Environment.Value("MyVariable")=10
  MyValue=Environment.Value("MyVariable")
Environment variables: Methods and Properties
Associated Methods and Properties
ExternalFileName Property
LoadFromFile Method
Value Property
ExternalFileName
It returns the name of the loaded external environment variable file specified in the Environment pane of the Test Settings dialog box.
If no external environment variable file is loaded, returns an empty string.
Syntax
Environment.ExternalFileName
Example:
'Check if an External Environment file is loaded and if not, load it.
fileName = Environment.ExternalFileName
If (fileName = "") Then
    Environment.LoadFromFile("C:\Environment.xml")
End If
'display value of one of the Environment variables from the External file
msgbox Environment("MyVarName")
LoadFromFile
It loads the specified environment variable file.
The environment variable file must be an XML file using the following syntax:
Syntax
Environment.LoadFromFile(Path)
Example:
  Environment.LoadFromFile("C:\QuickTest\Files\MyVariables.xml")
  Environment.LoadFromFile("C:\MyVariables.xml")
Value
  Sets or retrieves the value of environment variables.
  You can retrieve the value of any environment variable.
  You can set the value of only user-defined, environment variables.
  Syntax
  To set the value of a user-defined, environment variable:
  Environment.Value(VariableName) = NewValue
  To retrieve the value of a loaded environment variable:
  CurrValue = Environment.Value (VariableName)
  Example:
  Environment.Value("MyVariable")=10
  MyValue=Environment.Value("MyVariable")
Environment Variable file structure
<Environment>
<Variable>
       <Name> UN </Name>
       <Value>GAReddy</Value>
</Variable>
<Variable>
       <Name> Pwd</Name>
       <Value>GAReddy</Value>
</Variable>
</Environment> 
Environment Variables @ Scripts
Getting the Environment variables
  ' How to get the Environment variables
  myname=Environment.Value("name")
  'OR
  myname=Environment(“Uname")
  ‘Assigning Env Variable values
  Browser().Page().WebEdit(“userName”).set Environment.value(“Uname”)
  ‘OR
  Browser().Page().WebEdit(“userName”).set myname

Check if the specific variable exists in Env file
  'Checking if the specific variable exists on the Environment file
  On Error Resume Next
  myvar=Environment("name")
  If Err.Number <> 0 Then
  MsgBox "var does not exist"
  MsgBox err.number
  MsgBox Err.Value
  else
  MsgBox "var Exists"
  End If
Adding Environment variables dynamically
' Adding Environment variables dynamically
Environment("MyNewVar")="I am new Environment Variable"
' To check the above one
On Error Resume Next
myvar=Environment("MyNewVar")
If Err.Number <> 0 Then
MsgBox "var does not exist"
MsgBox err.number
MsgBox Err.Value
Environment("MyNewVar")="Iam new var"
else
MsgBox "var Exists"
End If
Empty the Environment value
Loading Environment files dynamically
' To empty the Environment value
Environment("MyVar")=Nothing
 ' Loading Environment files dynamically
 Environment.LoadFromFile EnvFile
Check if an External Environment file is loaded
  'Check if an External Environment file is loaded and if not, load it.
  fileName = Environment.ExternalFileName
  If (fileName = "") Then
  Environment.LoadFromFile("D:\EnvVars.xml")
  End If
  'display value of one of the Environment variables from the External file
  msgbox (Environment("UN"))
  '‘Second method
  On Error Resume Next
  fileName = Environment.ExternalFileName
  If Err.Number <> 0Then
      MsgBox "The Specified Environment File  does not exist"
  '    Environment.LoadFromFile("C:\EnvironmentFile.xml")
  Environment.LoadFromFile("D:\EnvVars.xml")
  Else
       MsgBox "The Specified Environment File exist"
  End If

Read More...

Friday, 10 December 2010

QTP - Action Driven Framework Demo 1

Leave a Comment
Read More...

Tuesday, 7 December 2010

QTP - RegularExpressions Info

Leave a Comment
QTP @ VBScript (Part 5)

Regular Expressions
Regular Expression is a string that describes or matches a set of strings
A regular expression is a way of expressing a text pattern for the purpose of matching a string or part of a string. Regular expressions are often used either to extract information from a string or to verify that a string is of the correct format
A regular expression is a string that specifies a complex search phrase.
Regular expressions enable QTP to identify objects and text strings with varying values
By using special characters you define the conditions of the search.
We can use this regular expression when we want to test for specific range of object properties 

Test for a pattern within a string. For example, you can test an input string to see if a telephone number pattern or a credit card number pattern occurs within the string. This called data validation.
Replace text. You can use a regular expression to identify specific text in a document and either remove it completely or replace it with other text.
Extract a substring from a string based upon a pattern match. You can find specific text within a document or input field

In QTP , Regular Expressions can be used in 4 places:
·         Object Repository
·         CheckPoints
·         RegExp object.
·         Data Validations 

Eleven characters with special meanings: the opening square bracket [, the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening round bracket ( and the closing round bracket ). These special characters are often called "metacharacters".
If you want to use any of these characters as a literal in a regex, you need to escape them with a backslash. If you want to match 1+1=2, the correct regex is 1\+1=2. Otherwise, the plus sign will have a special meaning

Character Classes or Character Sets
A "character class" matches only one out of several characters. To match an a or an e, use [ae]. You could use this in gr[ae]y to match either gray or grey.
A character class matches only a single character. gr[ae]y will not match graay, graey or any such thing. The order of the characters inside a character class does not matter.
You can use a hyphen inside a character class to specify a range of characters. [0-9] matches a single digit between 0 and 9. You can use more than one range. [0-9a-fA-F] matches a single hexadecimal digit, case insensitively. You can combine ranges and single characters. [0-9a-fxA-FX] matches a hexadecimal digit or the letter X.
Typing a caret after the opening square bracket will negate the character class.
The result is that the character class will match any character that is not in the character class. q[^x] matches qu in question. It does not match Iraq since there is no character after the q for the negated character class to match.
Shorthand Character Classes
\d matches a single character that is a digit, \w matches a "word character“ (alphanumeric characters plus underscore), and \s matches a whitespace character (includes tabs and line breaks). The actual characters matched by the shorthands depends on the software you're using. Usually, non-English letters and numbers are included.

The Dot Matches (Almost) Any Character
The dot matches a single character, except line break characters. It is short for [^\n] (UNIX regex flavors) or [^\r\n] (Windows regex flavors). Most regex engines have a "dot matches all" or "single line" mode that makes the dot match any single character, including line breaks. gr.y matches gray, grey, gr%y, etc. Use the dot sparingly. Often, a character class or negated character class is faster and more precise.

Anchors
Anchors do not match any characters. They match a position. ^ matches at the start of the string, and $ matches at the end of the string. Most regex engines have a "multi-line" mode that makes ^ match after any line break, and $ before any line break. E.g. ^b matches only the first b in bob. \b matches at a word boundary. A word boundary is a position between a character that can be matched by \w and a character that cannot be matched by \w. \b also matches at the start and/or end of the string if the first and/or last characters in the string are word characters. \B matches at every position where \b cannot match

Alternation
Alternation is the regular expression equivalent of "or". cat|dog will match cat in About cats and dogs. If the regex is applied again, it will match dog. You can add as many alternatives as you want, e.g.: cat|dog|mouse|fish.
Alternation is also good to test multilingual applications i.e. We a button Yes in English, Si in Spanish, Oui in French, Ja in Denish or German, we can use the next regular expression Yes|Si|Oui|Da|Ja

Repetition
The question mark makes the preceding token in the regular expression optional. E.g.: colou?r matches colour or color
The asterisk or star tells the engine to attempt to match the preceding token zero or more times. The plus tells the engine to attempt to match the preceding token once or more.
<[A-Za-z][A-Za-z0-9]*> matches an HTML tag without any attributes. <[A-Za-z0-9]+> is easier to write but matches invalid tags such as <1>.
Use curly braces to specify a specific amount of repetition. Use [1-9][0-9]{3} to match a number between 1000 and 9999. [1-9][0-9]{2,4} matches a number between 100 and 99999.

Regexp Object
The RegExp object provides support for regular expression matching; for the ability to search strings for substrings matching general or specific patterns.
In order to conduct a pattern search, you must first instantiate the regular expression object, with code like the following:
Dim oRegExp ' Instance of RegExp object
Set oRegExp = New RegExp

'Execute Method
'####################################################
' @ Executing a regular expression to find text within a string.
'####################################################
MsgBox RegularExpExample("QTP.", "QuickTestQTP, by GAReddy and QuickTestQTP is qtp's blog")
' =============================================================
' @Function:RegularExpExample
' @Desc : Using regular expression to find text within a string
' @Params :  strPattern is the regular expression
'            strString is the string to use the expression on
' @Returns : An example string showing the results of the search
' =============================================================
Function RegularExpExample(strPattern, strString)
Dim objRegEx, strMatch, strMatches
Dim strRet
' create regular expression object
Set objRegEx = New RegExp
' set the pattern
objRegEx.Pattern = strPattern
' set it be not case sensitive
objRegEx.IgnoreCase = True
' set global flag =true sothat we search all of the string, instead of just searching  for the first occurrence
objRegEx.Global = True
' execute search
Set strMatches = objRegEx.Execute(strString)
' for each match
For Each strMatch in strMatches
   strRet = strRet & "Match found at position '" & _
            strMatch.FirstIndex & "' - Matched Value is '" & _
            strMatch.Value & "'" & vbCRLF
Next
RegularExpExample = strRet
End Function ' RegularExpExample  

'Test Method
'#################################################################
  '  @Using Locate to determine if specific text exists within a string.
 '#################################################################
MsgBox LocateText("www.QuickTestQTP.blogpsot.com", "QTP")
MsgBox LocateText("www.QuickTestQTP.blogpsot.com", "QTP.*.com")
' =============================================================
' @Function:LocateText
' @Desc :   Uses a regular expression to locate text within a string
' @Params : strString is the string to perform the search on
'           strPattern is the regular expression
' @Returns : True if the pattern was found, False otherwise
' =============================================================
Function LocateText(strString, strPattern)
Dim objRegEx
' create the regular expression
Set objRegEx = New RegExp
' set the pattern
objRegEx.Pattern = strPattern
' ignore the casing
objRegEx.IgnoreCase = True
' perform the search
LocateText = objRegEx.Test(strString)
' destroy the object
Set objRegEx = Nothing
End Function ' LocateText 

'Replace Method
'#################################################################
  '@Using the replace method to find and replace text in a string.
 '#################################################################
MsgBox ReplaceText("QuickTest QTP: Learning QTP is easy .", "easy.", "easiest")
MsgBox ReplaceText("QTP seems to be  easy .Just look here, everything else about QTP exists: but implementation is HARD...!","but.*","!")
' =============================================================
' @Function: ReplaceText
' @Desc : Uses a regular expression to replace text within a string
' @Params :  strString is the string to perform the replacement on
'            strPattern is the regular expression
'            strReplacement is the replacement string
' @Returns : The finished string
' =============================================================
Function ReplaceText(strString, strPattern, strReplacement)
Dim objRegEx
' create the regular expression
Set objRegEx = New RegExp
' set the pattern
objRegEx.Pattern = strPattern
' ignore the casing
objRegEx.IgnoreCase = True
' make the replacement
ReplaceText = objRegEx.Replace(strString, strReplacement)
' destroy the object
Set objRegEx = Nothing
End Function ' ReplaceText


Thank you.
QTP – VBScript Part4  - Done.

Thank you.

For QTP and More:
Contact:
G A Reddy
http://QuickTestQTP.blogspot.com


QTP – VBScript (Part 1)
QTP – VBScript (Part 2)
QTP – VBScript (Part 3)
QTP – VBScript (Part 4)
QTP – VBScript (Part 5)
QTP – VBScript (Part 6)
QTP – VBScript (Part 7)
QTP – VBScript (Part 8)
QTP – VBScript Examples (Part9)
QTP – VBScript Examples (Part10)
QTP – Arrays
QTP – Error Handling
QTP – Functions
QTP – Frameworks
QTP – MORE….

To be Continued…
For more info
Contact@
G A Reddy
QuickTest QTP
http://QuickTestQTP.BlogSpot.com


Read More...

QTP – Regular Expressions

Leave a Comment
'RegExp
 Method
Dim strRet
' create regular expression object
Set objRegEx = New RegExp
' set the pattern
objRegEx.Pattern = strPattern
' set it be not case sensitive
objRegEx.IgnoreCase = True
' set global flag =true sothat we search all of the string, instead of just searching  for the first occurrence
objRegEx.Global = True
' execute search
Set strMatches = objRegEx.Execute(strString)
'=====================================================

'Execute Method
'####################################################
' @ Executing a regular expression to find text within a string.
'####################################################
MsgBox RegularExpExample("QTP.", "QuickTestQTP, by GAReddy and QuickTestQTP is qtp's blog")
' =============================================================
' @Function: RegularExpExample
' @Desc :    Example of how to use the regular expression object to find text within a string
' @Params :  strPattern is the regular expression
'            strString is the string to use the expression on
' @Returns : An example string showing the results of the search
' =============================================================
Function RegularExpExample(strPattern, strString)
Dim objRegEx, strMatch, strMatches
Dim strRet
' create regular expression object
Set objRegEx = New RegExp
' set the pattern
objRegEx.Pattern = strPattern
' set it be not case sensitive
objRegEx.IgnoreCase = True
' set global flag =true sothat we search all of the string, instead of just searching  for the first occurrence
objRegEx.Global = True
' execute search
Set strMatches = objRegEx.Execute(strString)
' for each match
For Each strMatch in strMatches
   strRet = strRet & "Match found at position '" & _
            strMatch.FirstIndex & "' - Matched Value is '" & _
            strMatch.Value & "'" & vbCRLF
Next
RegularExpExample = strRet
End Function ' RegularExpExample

'Test Method
'########################################################################
  '@Using Locate to determine if specific text exists within a string.
 '#######################################################################
MsgBox LocateText("www.QuickTestQTP.blogpsot.com", "QTP")
MsgBox LocateText("www.QuickTestQTP.blogpsot.com", "QTP.*.com")
' =============================================================
' @Function: LocateText
' @Desc :    Uses a regular expression to locate text within a string
' @Params :  strString is the string to perform the search on
'            strPattern is the regular expression
' @Returns : True if the pattern was found, False otherwise
' =============================================================
Function LocateText(strString, strPattern)
Dim objRegEx
' create the regular expression
Set objRegEx = New RegExp
' set the pattern
objRegEx.Pattern = strPattern
' ignore the casing
objRegEx.IgnoreCase = True
' perform the search
LocateText = objRegEx.Test(strString)
' destroy the object
Set objRegEx = Nothing
End Function ' LocateText

'Replace Method
'#################################################################
  '@Using the replace method to find and replace text in a string.
 '#################################################################
MsgBox ReplaceText("QuickTest QTP: Learning QTP is easy .", "easy.", "easiest")
MsgBox ReplaceText("QTP seems to be  easy .Just look here, everything else about QTP exists: but implemenation is HARD...!","but.*","!")
' =============================================================
' @Function: ReplaceText
' @Desc :    Uses a regular expression to replace text within a string
' @Params :  strString is the string to perform the replacement on
'            strPattern is the regular expression
'            strReplacement is the replacement string
' @Returns : ReplaceText
' =============================================================
Function ReplaceText(strString, strPattern, strReplacement)
Dim objRegEx
' create the regular expression
Set objRegEx = New RegExp
' set the pattern
objRegEx.Pattern = strPattern
' ignore the casing
objRegEx.IgnoreCase = True
' make the replacement
ReplaceText = objRegEx.Replace(strString, strReplacement)
' destroy the object
Set objRegEx = Nothing
End Function ' ReplaceText


Read More...

Tuesday, 14 December 2010

QTP - Environment Variables

QTP & Environment Variables

Welcome
  QTP – VBScript (Part 1)
  QTP – VBScript (Part 2)
  QTP – VBScript (Part 3)
  QTP – VBScript (Part 4)
  QTP – VBScript (Part 5)
  QTP – VBScript (Part 6)
  QTP – VBScript (Part 7)
  QTP – VBScript (Part 8)
  QTP – VBScript Examples (Part9)
  QTP – VBScript Examples (Part10)
  QTP – Environment Variables
  QTP – Arrays
  QTP – Error Handling
  QTP – Functions
  QTP – Frameworks
  QTP – MORE….
Contact@
G A Reddy
QuickTest QTP
http://QuickTestQTP.BlogSpot.com
Environment variables
Environment variables represents the QTP Environment object, which enables you to set or retrieve the value of environment variables
Environment variables are like global variables
Environment variables can be accessed through out from any part of the script.
The values of these variables remains same irrespective of the number of iterations (unless you change them through scripting).
These variables can prove to be very useful when you want a variable to be shared across various reusable actions/tests or functions.
Environment Variable - Types
Built-in
Built-In: These are the internal variables that are provided by QTP.
These provide valuable information like the path of the folder where test is located, the path of the results folder, the name of the action iteration or the OS version.
User Defined.
These can be further defined into two types.
User defined Internal
These are the variables that we define within the test.
These variables are saved with the test and are accessible only within the test in which they were defined.
Example:
AppURL=Environment.value(“URL”)=http://newtours.demoaut.com
SystemUtil.Run AppURL
User defined External
These are the variables that we predefine in the active external environment variables file.
These can be created using a list of variable-value pairs in an external file in .xml format.
Example:
MyEnvFile = “C:/EnvVars.xml”
Environment.LoadFromFile MyEnvFile
Browser().Page().WebEdit(“userName”).set Environment.value(“Uname”)
Environment variables
  To set the value of a user-defined, environment variable:
  Environment (VariableName) = NewValue
  To retrieve the value of a loaded environment variable:
  CurrValue = Environment (VariableName)
  Example:
  Environment.Value("MyVariable")=10
  MyValue=Environment.Value("MyVariable")
Environment variables: Methods and Properties
Associated Methods and Properties
ExternalFileName Property
LoadFromFile Method
Value Property
ExternalFileName
It returns the name of the loaded external environment variable file specified in the Environment pane of the Test Settings dialog box.
If no external environment variable file is loaded, returns an empty string.
Syntax
Environment.ExternalFileName
Example:
'Check if an External Environment file is loaded and if not, load it.
fileName = Environment.ExternalFileName
If (fileName = "") Then
    Environment.LoadFromFile("C:\Environment.xml")
End If
'display value of one of the Environment variables from the External file
msgbox Environment("MyVarName")
LoadFromFile
It loads the specified environment variable file.
The environment variable file must be an XML file using the following syntax:
Syntax
Environment.LoadFromFile(Path)
Example:
  Environment.LoadFromFile("C:\QuickTest\Files\MyVariables.xml")
  Environment.LoadFromFile("C:\MyVariables.xml")
Value
  Sets or retrieves the value of environment variables.
  You can retrieve the value of any environment variable.
  You can set the value of only user-defined, environment variables.
  Syntax
  To set the value of a user-defined, environment variable:
  Environment.Value(VariableName) = NewValue
  To retrieve the value of a loaded environment variable:
  CurrValue = Environment.Value (VariableName)
  Example:
  Environment.Value("MyVariable")=10
  MyValue=Environment.Value("MyVariable")
Environment Variable file structure
<Environment>
<Variable>
       <Name> UN </Name>
       <Value>GAReddy</Value>
</Variable>
<Variable>
       <Name> Pwd</Name>
       <Value>GAReddy</Value>
</Variable>
</Environment> 
Environment Variables @ Scripts
Getting the Environment variables
  ' How to get the Environment variables
  myname=Environment.Value("name")
  'OR
  myname=Environment(“Uname")
  ‘Assigning Env Variable values
  Browser().Page().WebEdit(“userName”).set Environment.value(“Uname”)
  ‘OR
  Browser().Page().WebEdit(“userName”).set myname

Check if the specific variable exists in Env file
  'Checking if the specific variable exists on the Environment file
  On Error Resume Next
  myvar=Environment("name")
  If Err.Number <> 0 Then
  MsgBox "var does not exist"
  MsgBox err.number
  MsgBox Err.Value
  else
  MsgBox "var Exists"
  End If
Adding Environment variables dynamically
' Adding Environment variables dynamically
Environment("MyNewVar")="I am new Environment Variable"
' To check the above one
On Error Resume Next
myvar=Environment("MyNewVar")
If Err.Number <> 0 Then
MsgBox "var does not exist"
MsgBox err.number
MsgBox Err.Value
Environment("MyNewVar")="Iam new var"
else
MsgBox "var Exists"
End If
Empty the Environment value
Loading Environment files dynamically
' To empty the Environment value
Environment("MyVar")=Nothing
 ' Loading Environment files dynamically
 Environment.LoadFromFile EnvFile
Check if an External Environment file is loaded
  'Check if an External Environment file is loaded and if not, load it.
  fileName = Environment.ExternalFileName
  If (fileName = "") Then
  Environment.LoadFromFile("D:\EnvVars.xml")
  End If
  'display value of one of the Environment variables from the External file
  msgbox (Environment("UN"))
  '‘Second method
  On Error Resume Next
  fileName = Environment.ExternalFileName
  If Err.Number <> 0Then
      MsgBox "The Specified Environment File  does not exist"
  '    Environment.LoadFromFile("C:\EnvironmentFile.xml")
  Environment.LoadFromFile("D:\EnvVars.xml")
  Else
       MsgBox "The Specified Environment File exist"
  End If

Tuesday, 7 December 2010

QTP - RegularExpressions Info

QTP @ VBScript (Part 5)

Regular Expressions
Regular Expression is a string that describes or matches a set of strings
A regular expression is a way of expressing a text pattern for the purpose of matching a string or part of a string. Regular expressions are often used either to extract information from a string or to verify that a string is of the correct format
A regular expression is a string that specifies a complex search phrase.
Regular expressions enable QTP to identify objects and text strings with varying values
By using special characters you define the conditions of the search.
We can use this regular expression when we want to test for specific range of object properties 

Test for a pattern within a string. For example, you can test an input string to see if a telephone number pattern or a credit card number pattern occurs within the string. This called data validation.
Replace text. You can use a regular expression to identify specific text in a document and either remove it completely or replace it with other text.
Extract a substring from a string based upon a pattern match. You can find specific text within a document or input field

In QTP , Regular Expressions can be used in 4 places:
·         Object Repository
·         CheckPoints
·         RegExp object.
·         Data Validations 

Eleven characters with special meanings: the opening square bracket [, the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening round bracket ( and the closing round bracket ). These special characters are often called "metacharacters".
If you want to use any of these characters as a literal in a regex, you need to escape them with a backslash. If you want to match 1+1=2, the correct regex is 1\+1=2. Otherwise, the plus sign will have a special meaning

Character Classes or Character Sets
A "character class" matches only one out of several characters. To match an a or an e, use [ae]. You could use this in gr[ae]y to match either gray or grey.
A character class matches only a single character. gr[ae]y will not match graay, graey or any such thing. The order of the characters inside a character class does not matter.
You can use a hyphen inside a character class to specify a range of characters. [0-9] matches a single digit between 0 and 9. You can use more than one range. [0-9a-fA-F] matches a single hexadecimal digit, case insensitively. You can combine ranges and single characters. [0-9a-fxA-FX] matches a hexadecimal digit or the letter X.
Typing a caret after the opening square bracket will negate the character class.
The result is that the character class will match any character that is not in the character class. q[^x] matches qu in question. It does not match Iraq since there is no character after the q for the negated character class to match.
Shorthand Character Classes
\d matches a single character that is a digit, \w matches a "word character“ (alphanumeric characters plus underscore), and \s matches a whitespace character (includes tabs and line breaks). The actual characters matched by the shorthands depends on the software you're using. Usually, non-English letters and numbers are included.

The Dot Matches (Almost) Any Character
The dot matches a single character, except line break characters. It is short for [^\n] (UNIX regex flavors) or [^\r\n] (Windows regex flavors). Most regex engines have a "dot matches all" or "single line" mode that makes the dot match any single character, including line breaks. gr.y matches gray, grey, gr%y, etc. Use the dot sparingly. Often, a character class or negated character class is faster and more precise.

Anchors
Anchors do not match any characters. They match a position. ^ matches at the start of the string, and $ matches at the end of the string. Most regex engines have a "multi-line" mode that makes ^ match after any line break, and $ before any line break. E.g. ^b matches only the first b in bob. \b matches at a word boundary. A word boundary is a position between a character that can be matched by \w and a character that cannot be matched by \w. \b also matches at the start and/or end of the string if the first and/or last characters in the string are word characters. \B matches at every position where \b cannot match

Alternation
Alternation is the regular expression equivalent of "or". cat|dog will match cat in About cats and dogs. If the regex is applied again, it will match dog. You can add as many alternatives as you want, e.g.: cat|dog|mouse|fish.
Alternation is also good to test multilingual applications i.e. We a button Yes in English, Si in Spanish, Oui in French, Ja in Denish or German, we can use the next regular expression Yes|Si|Oui|Da|Ja

Repetition
The question mark makes the preceding token in the regular expression optional. E.g.: colou?r matches colour or color
The asterisk or star tells the engine to attempt to match the preceding token zero or more times. The plus tells the engine to attempt to match the preceding token once or more.
<[A-Za-z][A-Za-z0-9]*> matches an HTML tag without any attributes. <[A-Za-z0-9]+> is easier to write but matches invalid tags such as <1>.
Use curly braces to specify a specific amount of repetition. Use [1-9][0-9]{3} to match a number between 1000 and 9999. [1-9][0-9]{2,4} matches a number between 100 and 99999.

Regexp Object
The RegExp object provides support for regular expression matching; for the ability to search strings for substrings matching general or specific patterns.
In order to conduct a pattern search, you must first instantiate the regular expression object, with code like the following:
Dim oRegExp ' Instance of RegExp object
Set oRegExp = New RegExp

'Execute Method
'####################################################
' @ Executing a regular expression to find text within a string.
'####################################################
MsgBox RegularExpExample("QTP.", "QuickTestQTP, by GAReddy and QuickTestQTP is qtp's blog")
' =============================================================
' @Function:RegularExpExample
' @Desc : Using regular expression to find text within a string
' @Params :  strPattern is the regular expression
'            strString is the string to use the expression on
' @Returns : An example string showing the results of the search
' =============================================================
Function RegularExpExample(strPattern, strString)
Dim objRegEx, strMatch, strMatches
Dim strRet
' create regular expression object
Set objRegEx = New RegExp
' set the pattern
objRegEx.Pattern = strPattern
' set it be not case sensitive
objRegEx.IgnoreCase = True
' set global flag =true sothat we search all of the string, instead of just searching  for the first occurrence
objRegEx.Global = True
' execute search
Set strMatches = objRegEx.Execute(strString)
' for each match
For Each strMatch in strMatches
   strRet = strRet & "Match found at position '" & _
            strMatch.FirstIndex & "' - Matched Value is '" & _
            strMatch.Value & "'" & vbCRLF
Next
RegularExpExample = strRet
End Function ' RegularExpExample  

'Test Method
'#################################################################
  '  @Using Locate to determine if specific text exists within a string.
 '#################################################################
MsgBox LocateText("www.QuickTestQTP.blogpsot.com", "QTP")
MsgBox LocateText("www.QuickTestQTP.blogpsot.com", "QTP.*.com")
' =============================================================
' @Function:LocateText
' @Desc :   Uses a regular expression to locate text within a string
' @Params : strString is the string to perform the search on
'           strPattern is the regular expression
' @Returns : True if the pattern was found, False otherwise
' =============================================================
Function LocateText(strString, strPattern)
Dim objRegEx
' create the regular expression
Set objRegEx = New RegExp
' set the pattern
objRegEx.Pattern = strPattern
' ignore the casing
objRegEx.IgnoreCase = True
' perform the search
LocateText = objRegEx.Test(strString)
' destroy the object
Set objRegEx = Nothing
End Function ' LocateText 

'Replace Method
'#################################################################
  '@Using the replace method to find and replace text in a string.
 '#################################################################
MsgBox ReplaceText("QuickTest QTP: Learning QTP is easy .", "easy.", "easiest")
MsgBox ReplaceText("QTP seems to be  easy .Just look here, everything else about QTP exists: but implementation is HARD...!","but.*","!")
' =============================================================
' @Function: ReplaceText
' @Desc : Uses a regular expression to replace text within a string
' @Params :  strString is the string to perform the replacement on
'            strPattern is the regular expression
'            strReplacement is the replacement string
' @Returns : The finished string
' =============================================================
Function ReplaceText(strString, strPattern, strReplacement)
Dim objRegEx
' create the regular expression
Set objRegEx = New RegExp
' set the pattern
objRegEx.Pattern = strPattern
' ignore the casing
objRegEx.IgnoreCase = True
' make the replacement
ReplaceText = objRegEx.Replace(strString, strReplacement)
' destroy the object
Set objRegEx = Nothing
End Function ' ReplaceText


Thank you.
QTP – VBScript Part4  - Done.

Thank you.

For QTP and More:
Contact:
G A Reddy
http://QuickTestQTP.blogspot.com


QTP – VBScript (Part 1)
QTP – VBScript (Part 2)
QTP – VBScript (Part 3)
QTP – VBScript (Part 4)
QTP – VBScript (Part 5)
QTP – VBScript (Part 6)
QTP – VBScript (Part 7)
QTP – VBScript (Part 8)
QTP – VBScript Examples (Part9)
QTP – VBScript Examples (Part10)
QTP – Arrays
QTP – Error Handling
QTP – Functions
QTP – Frameworks
QTP – MORE….

To be Continued…
For more info
Contact@
G A Reddy
QuickTest QTP
http://QuickTestQTP.BlogSpot.com


QTP – Regular Expressions

'RegExp
 Method
Dim strRet
' create regular expression object
Set objRegEx = New RegExp
' set the pattern
objRegEx.Pattern = strPattern
' set it be not case sensitive
objRegEx.IgnoreCase = True
' set global flag =true sothat we search all of the string, instead of just searching  for the first occurrence
objRegEx.Global = True
' execute search
Set strMatches = objRegEx.Execute(strString)
'=====================================================

'Execute Method
'####################################################
' @ Executing a regular expression to find text within a string.
'####################################################
MsgBox RegularExpExample("QTP.", "QuickTestQTP, by GAReddy and QuickTestQTP is qtp's blog")
' =============================================================
' @Function: RegularExpExample
' @Desc :    Example of how to use the regular expression object to find text within a string
' @Params :  strPattern is the regular expression
'            strString is the string to use the expression on
' @Returns : An example string showing the results of the search
' =============================================================
Function RegularExpExample(strPattern, strString)
Dim objRegEx, strMatch, strMatches
Dim strRet
' create regular expression object
Set objRegEx = New RegExp
' set the pattern
objRegEx.Pattern = strPattern
' set it be not case sensitive
objRegEx.IgnoreCase = True
' set global flag =true sothat we search all of the string, instead of just searching  for the first occurrence
objRegEx.Global = True
' execute search
Set strMatches = objRegEx.Execute(strString)
' for each match
For Each strMatch in strMatches
   strRet = strRet & "Match found at position '" & _
            strMatch.FirstIndex & "' - Matched Value is '" & _
            strMatch.Value & "'" & vbCRLF
Next
RegularExpExample = strRet
End Function ' RegularExpExample

'Test Method
'########################################################################
  '@Using Locate to determine if specific text exists within a string.
 '#######################################################################
MsgBox LocateText("www.QuickTestQTP.blogpsot.com", "QTP")
MsgBox LocateText("www.QuickTestQTP.blogpsot.com", "QTP.*.com")
' =============================================================
' @Function: LocateText
' @Desc :    Uses a regular expression to locate text within a string
' @Params :  strString is the string to perform the search on
'            strPattern is the regular expression
' @Returns : True if the pattern was found, False otherwise
' =============================================================
Function LocateText(strString, strPattern)
Dim objRegEx
' create the regular expression
Set objRegEx = New RegExp
' set the pattern
objRegEx.Pattern = strPattern
' ignore the casing
objRegEx.IgnoreCase = True
' perform the search
LocateText = objRegEx.Test(strString)
' destroy the object
Set objRegEx = Nothing
End Function ' LocateText

'Replace Method
'#################################################################
  '@Using the replace method to find and replace text in a string.
 '#################################################################
MsgBox ReplaceText("QuickTest QTP: Learning QTP is easy .", "easy.", "easiest")
MsgBox ReplaceText("QTP seems to be  easy .Just look here, everything else about QTP exists: but implemenation is HARD...!","but.*","!")
' =============================================================
' @Function: ReplaceText
' @Desc :    Uses a regular expression to replace text within a string
' @Params :  strString is the string to perform the replacement on
'            strPattern is the regular expression
'            strReplacement is the replacement string
' @Returns : ReplaceText
' =============================================================
Function ReplaceText(strString, strPattern, strReplacement)
Dim objRegEx
' create the regular expression
Set objRegEx = New RegExp
' set the pattern
objRegEx.Pattern = strPattern
' ignore the casing
objRegEx.IgnoreCase = True
' make the replacement
ReplaceText = objRegEx.Replace(strString, strReplacement)
' destroy the object
Set objRegEx = Nothing
End Function ' ReplaceText