-->Regular Expressions

Regular expressions are mathematical expressions used in QTP to generalize object properties which are consistently changing (dynamically changing) so that execution will be successful.
For eg-1; Say there is an application with classname javawindow and Edit box 'Title' and application belongs to version 1
In this case QTP captures test object info while learning as:
Class name : JavaWindow
Title : Customer details
Version : 1.0

And if we playback on the same application with a different version (say 1.1), the execution fails because of version mismatch. To generalize this we need to implement a regular expression for the version properties.
Eg: Regular expression to playback on the application with the version starting 1
Class name : JavaWindow
Title : Customer details
Version : 1.[0-9]*
Eg: Regular expression to playback on the application with any version
Class name : JavaWindow
Title : Customer details
Version : *.*
Or
Version : .*

Steps to implement Regular Expression:
1.Select Resources --> Object Repository
2.Select the required object from the left panel.
3.Select the required property from the right panel.
4.Click on configure the value option
5.Provide the regular expression for the value option
6.Enable the check button ‘Regular Expression’
7.Click on ‘No’ to not to add \ before & after special character
8.Click on OK --> Click on OK

Regular Expression Syntax:
* --> Matches the preceding character zero or more times.
Eg: "zo*" matches either "z" or "zoo".

+ --> Matches the preceding character one or more times.
Eg: "zo+" matches "zoo" but not "z".

. --> Matches any single character except a new line character

.* --> Any thing can repeat any number of times

? --> Matches the preceding character zero or one time only.
Eg: "a?ve?" matches the "ve" in "never".

x/y --> Matches either x or y.
Eg: "z|wood" matches "z" or "wood". "(z|w)oo" matches "zoo" or "wood".

{n} --> n is a non-negative integer which matches exactly n times.
Eg: "o{2}" does not match the "o" in "Bob," but matches the first two o's in "foooood".

{n,} --> n is a non-negative integer which matches at least n times.
Eg: "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 non-negative integers which matches at least n and at most m times.
Eg: "o{1,3}" matches the first three o's in "fooooood." "o{0,1}" is equivalent to "o?".

[xyz] --> A character set which matches any one of the enclosed characters in the set.
Eg: "[abc]" matches the "a" in "plain".

[^xyz] --> A negative character set which matches any characters that are not enclosed.
For example, "[^abc]" matches the "p" in "plain".

[a-z] --> A range of characters which matches any character in the specified range.
Eg: "[a-z]" matches any lowercase alphabetic character in the range "a" through "z".

[^a-z] --> A negative range of characters which matches any character not in the range.

\b --> Matches a word boundary, that is, the position between a word and a space.
Eg: "er\b" matches the "er" in "never" but not the "er" in "verb".

\B --> Matches a non-word boundary.
Eg: "ea*r\B" matches the "ear" in "never early".

\d --> Matches a digit character.
\d is equivalent to [0-9].

\D --> Matches a non-digit character.
\D is equivalent to [^0-9]

\w --> Matches any word character including underscore ( _ ).
\w is equivalent to [0-9 a-z A-Z _]

\W --> Matches any non-word character
\W is equivalent to [^ 0-9 a-z A-Z _]

Script to work with the Regular Expressions:
Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(strng)
For Each Match in Matches
RetStr = RetStr & "Match found at position "
RetStr = RetStr & Match.FirstIndex & ". Match Value is '"
RetStr = RetStr & Match.Value & "'." & vbCRLF
Next
RegExpTest = RetStr
End Function
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))

Eg: Regular Expression to validate an email id.
username@domainname.com
[0-9 a-z A-Z _ .]+ @ \w+ . [a-z A-z .]{2,3}