Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

Showing posts with label Regular Expressions. Show all posts
Showing posts with label Regular Expressions. Show all posts

List of Regular Expression for UFT/QTP

Regular expression are used to match input text with the pattern provided by regular expression engine. A pattern consists of one or more character literals, operators, or constructs. Below table explains the different regular expression patterns to identify the input text matches the expected pattern


Regular expression are used to validate the input text provided with the expected pattern. e.g : validating date format is correct or telephone number  provided is in correct format.



Reg. Expression

Description

( .)

 A dot matches single character (.) eg test. will match dot followed by any single character.example: testa, test5 

 [ab]

Matches either of the character in the list. e.g 23[ab] will match both 23a or 23b but not 23r

[^ab]

Matches all the characters excluding the the character in the list. e.g 23[^ab] will not match both 23a or 23b but will match23r,235 or 23 followed by any character other

 \b

Matches a word at boundary. e.g test/b will match entest but not entester

 [x-y]

Matches a single character in the range. e.g 23[a-d] will match 23a,23b,23c,23d but not 23r

 [^x-y]

Matches a single character not in the range. e.g 23[^a-d] will not match 23a,23b,23c,23d but 23r

 \B

Matches a word not at boundary. e.g test/b will match entest but not entester

 \W

Matches any non-alphaNumeric character excluding underscore

 \w

Matches any alphaNumeric character including underscore

*

Wildcard character matching the multiple occurence of preceding character. e.g rat* will match ra, rat, rattt, ............i.e multiple occurence of preceeding character t in this example.

 .*

Wildcard character matching the multiple occurence of preceding character.Preceeding character is . in case, so ra.* will match any string starting with ra

+

Similar to * but atleast one occurence of the character should match.e.g : rat+ will match rat but not ra

 ?

 matches zero or one occurrences of the preceding character. For example: ra?t match rt or rat only

 |

Matches either of the character seperated by |.example nes(t|l)ey will match nestey or nesley.

 \d

Matches a digit character

\D

Matches a non- digit/numeric character

\

marks the next character as special character or a literal. e.g \n is for new line ; \\ for \ and \d for a digit character

^

matches the pattern at start of line. e.g: ^test will match testing, tester but not autotest.

 $

matches the pattern at end of line. e.g: test$ will match autotest but not tester.

 |

Matches either of the character seperated by |.example nes(t|l)ey will match nestey or nesley.

 {}

Matches the start and end of qualifier expression. e.g a{4,5} will match aaaa or aaaaa

Reference:  Regular Expression Language - Quick Reference

RegExp Object for regular expression using VBScript in QTP

VBScript provides a RegExp object to handle regular expressions.This object allows you to find regular expression matches in strings, and replace regex matches in strings with other strings.

Properties of RegExp Object

Following are the properties of regexp object:

IgnoreCase:  By default regular expression is case sensitive, to make it case insensitive, set value as “True”

Pattern: We can define the pattern of regular expression in the pattern property. Pattern can include literal and meta characters like .,*,/d as discussed in the earlier post on how to create regular expressions in QTP

Global: To return or replace all matches, set Global as True. If set as False, finds the first match only


‘’Defining a regular expression
Set newRegExp = New RegExp
newRegExp.IgnoreCase = True
newRegExp.Global = True
newRegExp.Pattern = ".*an"

Methods of RegExp object


Execute method: executes a match against the specified string. Returns a Matchescollection, which contains a Match object for each match. The Matchobject can also contain SubMatches collection. 

Execute method has following properties:

o    Item

       §  Item.value – value of item in the collection
       §  Firstindex – First instance of match location

o    Count

Count of instances matching the regular expression

Replace method: replaces the part of the string found in a match with another string.Does replace the pattern defined for object with the replace value.                                          Syntax: regExpobj.replace(teststring,”newpattern”)

Test method: executes an attempted match and returns True or False based on match found.


Sample Code explaining properties and methods of RegExp Object.


Set newRegExp = New RegExp
newRegExp.IgnoreCase = True
newRegExp.Global = True
newRegExp.Pattern = ".*an"
testString = "this ant is an insect"
Set colmatch = newRegExp.Execute(teststring)
For each match in colmatch
         msgbox match.Value
        msgbox colmatch.count
Next
boolMatch = newRegExp.Test(teststring)
msgbox boolMatch
newRegExp.pattern ="a"
strString = newRegExp.replace(teststring,"b")
msgbox strstring


To understand more on how to use regular expressions in qtp, click here


How to use Regular Expressions in QTP


Regular expressions are used to identify objects and text strings with varying values. Regular Expression are strings that defines search phrase based on special character provided in the expression.This is useful when expected value of any object property is regularly changing but in a fix pattern.

Some useful points to remember about Regular Expressions are:


  • Regular Expression is useful for following scenarios in QTP:
    • Defining the property values of an object in dialog boxes or in programmatic descriptions
    • Defining expected values for checkpoints
    • Defining pop-up window conditions in a recovery scenario.
  • Can create regular expression for strings only.
  • Period (.), hyphen (-), asterisk (*), caret (^), brackets ([ ]),parentheses (()), dollar sign ($), vertical line (|), plus sign (+), question mark (?), and backslash (\) are special characters used to create regular expression.
  • When one of above mentioned special characters is preceded by a backslash (\), QTP treats it as a literal character.
  • By default, the value of all Property objects added to a Properties collection are treated as regular expressions.

Below are the various regular expressions used in QTP.

  • Matching Any Single Character (.) eg abc. Will match abc followed by any character.

  • Matching Any Single Character in a List ( [xy] ) e.g [ab] will match either a or b

  • Matching Any Single Character Not in a List ( [^xy] ) e.g 1[^23] will match all values between 11 to 19 except 12 and 13.

  • Matching Any Single Character within a Range ( [x-y] ) e.g : 1[1-3] will match 11,12, and 13.

  • Matching Any AlphaNumeric Character Including the Underscore ( \w )

  • Matching Any Non-AlphaNumeric Character (\W) will match any special character other than underscore. Please note case of W in this case.

  • Matching Zero or More Specific Characters ( * ) This matches zero or more occurrences of the preceding character. e.g ca* will match caa,caaaa,c and so on. Similarly c.* will match c, cs,caaa, and so on, since preceding character here is “.”.

  • Matching One or More Specific Characters ( + ) Only difference from * is it will match for minimum one character. e.g ta+r will match taar,tar but not tr as in above case.

  • Matching Zero or One Specific Character ( ? ) A question mark (?) instructs QTP to match zero or one occurrences of the preceding character. For example: te?r matches ter and tr, but nothing else

  • Matching One of Several Regular Expressions ( | )  e.g new|day will match either of new or day. If we write ne(w|d)ay, it will match neway or neday.

  • Matching the Beginning of a Line ( ^ ) This will match only if match is found at beginning of line.

  • Matching the End of a Line ( $ )  This will match only if match is found at end of line.

  • Matching a word at boundary(\b) e.g new\b will match testnew but not in knewit.

  • Matches a digit character(\d)  Matches a digit value.

  • Matching a non-digit character(\D) Matches a non digit value

For understanding of regexp object for regular expression, Click here