UFT/QTP – Regular Expression

A regular expression (also known as regexp and regex) is a text that consists of patterns of characters for describing a search pattern.

Regular Expression Characters

Character Description
\ Marks the next character as either a special character or a literal. For example, “n” matches the character “n”. “\n” matches a newline character. The sequence “\\” matches “\” and “\(” matches “(“.
^ Matches the beginning of input.
$ Matches the end of input.
* Matches the preceding character zero or more times. For example, “zo*” matches either “z” or “zoo”.
+ Matches the preceding character one or more times. For example, “zo+” matches “zoo” but not “z”.
? Matches the preceding character zero or one time. For example, “a?ve?” matches the “ve” in “never”.
. Matches any single character except a newline character.
(pattern) Matches pattern and remembers the match. The matched substring can be retrieved from the resulting Matches collection, using Item [0]…[n]. To match parentheses characters ( ), use “\(” or “\)”.
x|y Matches either x or y. For example, “z|wood” matches “z” or “wood”. “(z|w)oo” matches “zoo” or “wood”.
{n} n is a nonnegative integer. Matches exactly n times. For example, “o{2}” does not match the “o” in “Bob,” but matches the first two o’s in “foooood”.
{n,} n is a nonnegative integer. Matches at least n times. For example, “o{2,}” does not match the “o” in “Bob” and matches all the o’s in “foooood.” “o{1,}” is equivalent to “o+”. “o{0,}” is equivalent to “o*”.
{n,m} m and n are nonnegative integers. Matches at least n and at most m times. For example, “o{1,3}” matches the first three o’s in “fooooood.” “o{0,1}” is equivalent to “o?”.
[xyz] A character set. Matches any one of the enclosed characters. For example, “[abc]” matches the “a” in “plain”.
[^xyz] A negative character set. Matches any character not enclosed. For example, “[^abc]” matches the “p” in “plain”.
[a-z] A range of characters. Matches any character in the specified range. For example, “[a-z]” matches any lowercase alphabetic character in the range “a” through “z”.
[^m-z] A negative range characters. Matches any character not in the specified range. For example, “[m-z]” matches any character not in the range “m” through “z”.
\b Matches a word boundary, that is, the position between a word and a space. For example, “er\b” matches the “er” in “never” but not the “er” in “verb”.
\B Matches a non-word boundary. “ea*r\B” matches the “ear” in “never early”.
\d Matches a digit character. Equivalent to [0-9].
\D Matches a non-digit character. Equivalent to [^0-9].
\f Matches a form-feed character.
\n Matches a newline character.
\r Matches a carriage return character.
\s Matches any white space including space, tab, form-feed, etc. Equivalent to “[ \f\n\r\t\v]”.
\S Matches any nonwhite space character. Equivalent to “[^ \f\n\r\t\v]”.
\t Matches a tab character.
\v Matches a vertical tab character.
\w Matches any word character including underscore. Equivalent to “[A-Za-z0-9_]”.
\W Matches any non-word character. Equivalent to “[^A-Za-z0-9_]”.
\num Matches num, where num is a positive integer. A reference back to remembered matches. For example, “(.)\1” matches two consecutive identical characters.
\n Matches n, where n is an octal escape value. Octal escape values must be 1, 2, or 3 digits long. For example, “\11” and “\011” both match a tab character. “\0011” is the equivalent of “\001” & “1”. Octal escape values must not exceed 256. If they do, only the first two digits comprise the expression. Allows ASCII codes to be used in regular expressions.
\xn Matches n, where n is a hexadecimal escape value. Hexadecimal escape values must be exactly two digits long. For example, “\x41” matches “A”. “\x041” is equivalent to “\x04” & “1”. Allows ASCII codes to be used in regular expressions.

Grouping Regular Expressions – ()

Multiple regular expression characters can be combined in a single expression.                                   Example – Window(“text:=.*(Report|Process)”).Close

The above line closes a window with text Report or Process preceded by any set of alphanumeric characters.

Regexp Object

RegExp object helps the developers to match the pattern of strings and the properties and methods help us to work with Regular Expressions easily.

Regexp Rx = New Regexp

Regular Expression Properties

  • Pattern – Sets or returns the regular expression pattern being searched for.
  • IgnoreCase – A Boolean property that represents if the regular expression should be tested against all possible matches in a string if true or false. Set the IgnoreCase property to true to make it case insensitive.
  • Global – A Boolean property that represents if the regular expression should be tested against all possible matches in a string. Default Global value is set to False.

Regular Expression Method

  1. Test –  The test method takes one parameter as a string to test the regular expression on. Test returns True or False, indicating if the regular expression matches the string.
    Syntax – myVar = object.Test(string)
    Example –
    Dim myRegEx, myVal,String String = “Automation tutorial of UFT “Set myRegEx = New regExpmyRegEx.Pattern = “UFT”               ‘Set PatternmyRegEx.IgnoreCase = False          ‘Set case sensitivitymyVal = myRegEx.Test(String)If myVal ThenPrint(“Match is found”)

    Else

    Print(“No Match was found”)

    End If

  2. Execute – The Execute method also takes one parameter as a string. Instead of returning True or False, it returns a matches collection object. If Global property is False, matches collection will contain only the first match. If Global property is True, matches collection will contain all matches.
    Syntax – object.Execute(String)
    Example
    Dim myRegEx, Match,Matches,String,retStringString = ” UFT Uft, uft, QTP”Set myRegEx = New regExpmyRegEx.Pattern = “UFT”               ‘Set PatternmyRegEx.IgnoreCase = True                       ‘Set case insensitivitymyRegEx.Global = True                   ‘Set Global Applicable

    Matches = myRegEx.Execute(String)

    For each Match in Matches

    retString = retString & “Match found at position ”

    retString = retString & Match.FirstIndex & “. Match value is ”

    retString = retString & Match.value & “.” & vbcrlf

    Next

    Print(retString)

  3. Replace – The Replace method takes two string parameters. The first parameter is the text string in which the text replacement is to occur, while the second parameter is the replacement text.  If Global property is False, Replace will return the subject string with first match replace. If Global property is True, Replace will return the subject string with all matches replaced.
    Syntax – object.Replace(String1, String2)
    Example
    Dim myRegEx, String1, String2, newStringString1 = “Hello Dear, Good Morning”String2 = “Hi”Set myRegEx = New regExpmyRegEx.Pattern = “Hello”             ‘Set Pattern

    myRegEx.IgnoreCase = True                       ‘Set case insensitivity

    myRegEx.Global = True                   ‘Set Global Applicable

    newString = myRegEx.Replace(String1, String2)

    Print(“String before replace ”& String1)

    Print(“String after replace ”& newString)

Matches Collection Object

The Matches collection object is returned as a result of the Execute method. This collection object can contain zero or more Match objects and the properties of this object are read-only.

  • Count – The Count method represents the number of match objects in the collection.
  • Item – The Item method enables the match objects to be accessed from matches collections object.

Match Object

The Match object is contained within the matches collection object. These objects represent the successful match after the search for a string.

  • FirstIndex – It represents the position within the original string where the match occurred. This index are zero-based which means that the first position in a string is 0.
  • Length – A value that represents the total length of the matched string.
  • Value – A value that represents the matched value or text. It is also the default value when accessing the Match object.

Example 1: Regular expression to match all valid email addresses

^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$

We have assumed above that the TLD (.com, .org, .net, .in) is between 2 to 4 characters long. Also, we have only taken the case for lower case characters for validation.

Example 2: Regular expression to match a date in MM/DD/YYYY format

^([1-9]|1[0-2])/([1-9]|[1-2][0-9]|3[0-1])/[0-9][0-9][0-9][0-9]$

Leave a Reply Cancel reply