QTP - RegularExpressions Info
QTP @ VBScript (Part 4)
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
1 comments:
Iam very glad to see total stuff of qtp at one place. nice explanation for all topics. good effort.
Post a Comment