Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Regular Expressions in QTP
Regular Expressions in QTP

Regular Expressions in QTP:

What is Regular Expression?

It is a way of representing data using symbols. They are often used within matching, searching or replacing algorithms.

Regular Expressions in QTP:

Regular expressions can be used in QTP for identifying objects and text strings with varying values.
Where we use:
o Defining the property values of an object in Descriptive programming for  handling dynamic objects
o For parameterizing a step
o creating checkpoints with varying values

Using Regular Expressions in QTP:

We can define a regular expression for a constant value, a Data Table parameter value, an Environment parameter value, or a property value in Descriptive programming.

We can define a regular expression in standard checkpoint to verify the property values of an object; we can set the expected value of an object’s property as a regular expression so that an object with a varying value can be verified.

We can define the text string as a regular expression, when creating a text checkpoint to check that a varying text string is displayed on our application,
For XML checkpoints we can set attribute or element values as regular expressions.

Ways of Regular Expressions:

Backslash Character:

A backslash () can serve two purposes. It can be used in conjunction with a special character to indicate that the next character be treated as a literal character.
Alternatively, if the backslash () is used in conjunction with some characters that would otherwise be treated as literal characters, such as the letters n, t, w, or d, the combination indicates a special character.

Matching Any Single Character:

A period (.) instructs QTP to search for any single character (except for n).
Ex:
welcome.
Matches welcomes, welcomed, or welcome followed by a space or any other single character.

Matching Any Single Character in a List:

Square brackets instruct QTP to search for any single character within a list of characters.
Ex:
To search for the date 1867, 1868, or 1869, enter:

186[789]

Matching Any Single Character Not in a List:

When a caret (^) is the first character inside square brackets, it instructs QTP to match any character in the list except for the ones specified in the string.
Example:
[^ab]
Matches any character except a or b.

Matching Any Single Character within a Range:

To match a single character within a range, we can use square brackets ([ ]) with the hyphen (-) character.
Example:
For matching any year in the 2010s, enter:

201[0-9]

Matching Zero or More Specific Characters:

 An asterisk (*) instructs QTP to match zero or more occurrences of the preceding character.
For example:
ca*r
Matches car, caaaaaar, and cr

Matching One or More Specific Characters:

 A plus sign (+) instructs QTP to match one or more occurrences of the preceding character.
For example:
ca+r
Matches car and caaaaaar, but not cr.

Matching Zero or One Specific Character:

A question mark (?) instructs QTP to match zero or one occurrences of the preceding character.
For example:
ca?r
Matches car and cr, but nothing else.

Grouping Regular Expressions:

Parentheses (()) instruct QTP to treat the contained sequence as a unit, just as in mathematics and programming languages. Using groups is especially useful for delimiting the argument(s) to an alternation operator ( | ) or a repetition operator ( * , + , ? , { } ).

Matching One of Several Regular Expressions:

 A vertical line (|) instructs QTP to match one of a choice of expressions.

Matching the Beginning of a Line:

A caret (^) instructs QTP to match the expression only at the start of a line, or after a newline character.

Matching the End of a Line: 

A dollar sign ($) instructs QTP to match the expression only at the end of a line, or before a newline character.

Matching Any AlphaNumeric Character Including the Underscore:

 w instructs QTP to match any alphanumeric character and the underscore (A-Z, a-z, 0-9, _).

Matching Any Non-AlphaNumeric Character:

 W instructs QTP to match any character other than alphanumeric characters and underscores.

Combining Regular Expression Operators: 

We can combine regular expression operators in a single expression to achieve the exact search criteria we need.
For example,
start.*
Matches start, started, starting, starter, and so forth.
we can use a combination of brackets and an asterisk to limit the search to a combination of non-numeric characters.
For example:
[a-zA-Z]*
To match any number between 0 and 1200, we need to match numbers with 1 digit, 2 digits, 3 digits, or 4 digits between 1000-1200.
The regular expression below matches any number between 0 and 1200.
([0-9]?[0-9]?[0-9]|1[01][0-9][0-9]|1200)
RegExp object

VB Script is providing RegExp object for defining Regular expressions, It provides simple support for defining regular expressions.
Regular Expression Object Properties and Methods:
 Properties:
 a) Global Property
 b) IgnoreCase Property
 c) Pattern Property
 Methods:
 a) Execute Method
 b) Replace Method
 c) Test Method
Regular Expressions Examples:

1) Match File Names in a Directory against Regular Expression


Set objFS = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strCurrentDirectory = objShell.CurrentDirectory

Set objFolder = objFS.GetFolder(strCurrentDirectory)
Set colFiles = objFolder.Files

Set objRE = New RegExp
objRE.Global     = True
objRE.IgnoreCase = False
objRE.Pattern    = WScript.Arguments(0)

For Each objFile In colFiles
   bMatch = objRE.Test(objFile.Name)
   If bMatch Then
      WScript.Echo objFile.Name
   End If
Next
 

2) Match Content in a File against a Regular Expression


strFileName = "E:test.txt"
Set objFS = CreateObject("Scripting.FileSystemObject")

Set objTS = objFS.OpenTextFile(strFileName)
strFileContents = objTS.ReadAll
WScript.Echo "Searching Within: "
WScript.Echo strFileContents
objTS.Close

Set objRE = New RegExp
objRE.Global     = True
objRE.IgnoreCase = False
objRE.Pattern    = WScript.Arguments(0)

Set colMatches = objRE.Execute(strFileContents)
WScript.Echo vbNewLine & "Resulting Matches:"
For Each objMatch In colMatches
WScript.Echo "At position " & objMatch.FirstIndex & " matched " & objMatch.Value
 

Regular Expression cheat seat:

CharacterDescription
Indicates that the next character is either special character or literal
^Indicates a match at the beginning of the input
$Indicates a match at the end of the input
*Indicates a match before the indicated character.Matches will be performed either zero or more times.
+Indicates a match before the indicated character.Matches will be performed either one or more times.
?Indicates a match before the indicated character.Matches will be performed either zero or one time.It can also matches any single character except a newline character.
(pattern)Indicates a match with the given pattern.It returns a collection of items.To retrieve the pattern info we can use item[0],item[1],….item[n]
(?:pattern)This is a non Capturing search hence the match is not captures for future use.It is useful for putting OR condition
(?=pattern)This is a non Capturing search hence the match is not captures for future use.However it is a positive look head that matches the search string at any given point of time.
Test(?1,2,3,8,9) matches Test in TestCase1,TestCase2…TestCase9 but not in TestData
(?!pattern)This is a non Capturing search hence the match is not captures for future use.However it is a negative look head that matches the search string at any given point of time.
Test(?!1,2,3,8,9) matches Test in TestData but not in TestCase1,TestCase2…TestCase9
x|yIndicates a match with a match will happen with either x or y
{n}Indicates a match will happen exactly n times,where n is greater than zero. 
{n,}Indicates a match will happen at least n times where n is greater than zero and it must terminate with a comma.
{n,m}Indicates a match will happen at least n times and at most m times where n and m are greater than zero and m is also greater than n.
[xyz]Indicates a match with any character from the given character set.
[^xyz]Indicates a match with any other character which are not part of the given character set.
[a-z]
[A-Z]
Indicates a match with  any character in the given range.
[^m-z]Indicates a match with a negative range of character.
bIndicates a match with a boundary for word.It identifies the positioning between a word and a space. 
SIndicates a match with all non white space just opposite to /s
BIndicates a match with a non word Boundary.
dIndicates a match with a digit character.
DIndicates a match with a non digit character.
fIndicates a match with a form feed character.
nIndicates a match with a new line character
rIndicates a match with a carriage return character
sIndicates a match with any white space character including space,tab etc. It is equivalent to
[fnrtv]
wIndicates a match with the word character including underscore
[A-Za-z0-9_]
tIndicates a match with a tab character
WIndicates a match with the non word character like [^A-Za-z0-9_]
.Indicates a match with the dot
|Indicates a match with a Pipe
{Indicates a match with the curly bracket start”{“
}Indicates a match with the curly bracket end “}”
\Indicates a match with backslash
()
[Indicates a match with the third bracket start “[“
]Indicates a match with the third bracket end “]”
(Indicates a match with the first bracket start “(“
)Indicates a match with the first bracket end “)”
$numIndicates a match with a positive number.
vIndicates a match with a vertical tab character
nIndicates a match with an Octal value may be 1,2,3 digits
uxxxxIndicates a match with an ASCII character represented by Unicode-XXXX
xnIndicates a match with n,where n is a hexadecimal value of 2 digits
[m-z]Indicates a match with a character not in the given range.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here