Regular Expressions Regularized!!! | Quick Test Professional(QTP)
.
QTP Blog RSS

Regular Expressions Regularized!!!

A regular expression is a string that describes or matches a set of strings. It is often called a pattern as it describes set of strings.

Given underneath is one of the most widely used and ever confused BackLash character. The remaining expressions are serialized below that.

Using the Backslash Character
A backslash (\) instructs QuickTest to treat the next character as a literal character, if it is otherwise a special character. The backslash (\) can also instruct QuickTest to recognize certain ordinary characters as special characters. For example, QuickTest recognizes \n as the special newline character.
For example:
w matches the character w
\w is a special character that matches any word character including underscore
For example, in QTP, while entering the URL of a website,
http://mercurytours.mercuryinteractive.com
The period would be mistaken as an indication of a regular expression. To indicate that the period is not part of a regular expression, you would enter it as follows:
mercurytours\.mercuryinteractive\.com Note: If a backslash character is used before a character that has no special meaning, the backslash is ignored. For example, \z matches z.

Expressions & Explanation
Special characters and sequences are used in writing patterns for regular expressions. The following describes the characters and sequences that can be used.


\
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 "\)".

xy
Matches either x or y. For example, "zwood" matches "z" or "wood". "(zw)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.

Reference: QTP Documentation-Pattern property

If you want to keep track of further articles on QTP. I recommend you to subscribe via RSS Feed. You can also subscribe by Email and have new QTP articles sent directly to your inbox.

Note: Please use the Testing Tools forum for posting questions now!

32 comments:

swanandk@techmahindra.com said...

Hi Ankur,
Can we use regular expression in recovery scenarios ?
I have a condition where a pop-up window appears randomly, but the window name has application name followed by some error message (say, "ABC - No data found").
I want to use regular expression in recovery scenario saying if pop-up window appears having ABC in the window name, then click "OK" & proceed further.
I tried a lot, but was unable to do it. Is there something that i am missing ?

Ankur said...

Hi Swanandk,
Yes you can use regular expression in recovery scenarios. For your specific case try to use \w* after your application name.(Dont forget to check the Regular expression check-box...while defining RS for pop-up window title name) This will match any word character 0 or more times.
Let me know if it solves your problem

chitra said...

hi Ankur
can u plz tell me the concept of Regular expression here in QTP,as we use it for variying Window lables in Winrunner,then In QTP we use it for same purpose
plz reply me how to do it and why

Ankur said...

@ Chitra
The concept of regular expression is same everywhere be it win runner/ QTP or what that matter any scripting or programming language.

You can refer the above post to get yourself familiarized with regex.

chitra said...

hi ankur
thanks 4 ur help
due to that i am successful in doing reg exp

shetty said...

hi ankur,
how to use regular expression to write coding for version controlling.

Raji said...

Hi Ankur,
Can you tell me how to write regular expression for a string "Fax Order No. 211" where the number 211 changes dynamically after each run.
In this case the check point inserted should be output valu or standard check point?

Ankur said...

@ Raji,

There are many ways to solve the above problem.

You can use
i)Fax Order No. [0-9][0-9][0-9]
OR
ii)Fax Order No. [0-9]{3} //if the digits are exactly equal to 3//
OR
iii) Fax Order No. [0-9]{3,} //if the digits are equal to or greater than 3

Raji said...

It did not work for me.throw an error.

//Cannot find the "Fax Order No. 84" object's parent "Flight Reservation" (class Window). Verify that parent properties match an object currently displayed in your application.//

can you tell me the meaning of this eeror.I inserted a std check point for the dialogue box caption "Fax Order No. ".Is this the correct way to do this?

Swapna said...

Hi,

I am bit confused on reg exp.

My code is 'SwfWindow("frmMain").SwfWindow("Lead Profile").SwfLabel("008-16"). Click 80,5 and I replaced with the reg exp to

'SwfWindow("frmMain").SwfWindow("Lead Profile").SwfLabel("[0-9][0-9][0-9]-[0-9][0-9]").Click 80,5


I get the error "The "[0-9][0-9][0-9]-[0-9][0-9]" SwfLabel object was not found in the Object Repository.
Check the Object Repository to confirm that the object exists or to find the correct name for the object."

The object exists in OR with object name "008-16" and class swfLabel.

How do I over come this issue?

Vikas said...

Hi Raji

Use this expression

Fax Order No. [0-9]{1,}


Now it will match all the fax order numbers which has 1 or more digits.

Vikas said...

Hi swapna,

I dont think regular expressions work in the place where you have used it.

What i meant was
it cant be used in the statement
'SwfWindow("frmMain").SwfWindow("Lead Profile").SwfLabel("[0-9][0-9][0-9]-[0-9][0-9]").Click

Instead use it in the "Name" property of the object "SwfLabel" in Object repository.

change the "Name" in OR as
[0-9]{3}\-[0-9]{1,}

which means , it will recognise all those labels which have a name
consisiting of
3 digit number
followed by a "-"
followed by a number which has 1 digit or more

lingam said...

Hi Ankur

This is kiran. i am working as test engineer in vission apps.I am appriciating u r work. this is very useful to all who are coming to testing field. I want u r id. plz send to kiranreddy.lingam@gmail.com

buy ankur

ur brother
kiran

Ankur said...

@ Vikas: Thanks for answering the queries here. I need help from people like you to keep this blog alive. Going with the volume of question being asked here daily and my work time lines, at times it becomes difficult for me to keep track.

Ankur said...

@ lingam: You can post your questions here itself.

Swapna said...

Thank you Vikas, It worked.

This was very quick......... thank you once again

Anonymous said...

Hi Ankur,

Can you please tell me how to use regular expression to verify numbers (non-zero). I tried "[1-9].*" but it didnt work. I was to ensure its a valid number of any digits and not zero.

Anonymous said...

Hi Ankur ,
On my webpage there is a webelement having text "Pre-Authorize Basic Loan Information(Write Access) - Exec Time: 497 ms -"
The last part of the string (497 ms -")changes always , say "540 ms -" , "650 ms -" depending on exec time . Rest of the string remains same. I want to write code :
If Browser().Page().Frame().Webelement("text:=Authorize Basic Loan Information(Write Access) - Exec Time:****
").Exist(10) Then
msgbox "Pass"
Else
msgbox "Fail"
End If
Please guide me how I can use regular expression here for the last part of string .

Sreenu said...

Hi,

Regular expression won't work in script editing. you need to modify the OR with the regular expression.

Regards,
Sreenivas

Anonymous said...

HI Ankur,
i want to use Regular expression on sieble application. we have different environments like Development, Systems, Training, Testing etc… of the same application
Now my problem, I have to prepare a basic script and I need to modify the script and Object repository to work on all the environments.

Here my difficulty is:
Browser(“Sieble- Systems Application”). Page(“Sieble- Systems Application”).Frame(“Sieble – Systems Application”).Button ( “New”).Click

For Development environment
Sieble- Development Application
For Training
Sieble- Training Application
For Testing
Sieble- Testing Application
Looks like that can u suggest me how to use Regular expression in this case and similarly what type of modification I need to do in the Object repository also.

“Systems Application” à This part I need to parameterize for different environments.

Looking forward for your Help in this regard.
rkelchuri@yahoo.com

Ankur said...

Hi All,

I have stopped taking questions on this blog. Please use Testing Tools forum for posting QTP questions.

reva said...

hi
i want to use regular expression to find no is in the range [100-632]could you please help
thanks
reva raghavan

bhanu said...

Hai all
I want some information regarding webtable,i want to check a web table cell's font size please help me to do this i did't find any functions in webtable object and vbscript.

Chetan-24 said...

Hi,
i have included reguler expression but its not working
could anyone please spacify how can validate my email address

Chetan

Anonymous said...

I need to get an example for how to write a regular expression for any simple object in "Flight Reservation" Application.hope anyone will guide me with a proper example as soon as possible.

Anonymous said...

please write down a small script as an example for understanding about how to use regular expression.

Chanikya said...

Hi Ankur,
I had a question here.
How do I go to a particular line of the code from other part of the code without any error?
Actually, we use on error resume next when an error occurs. But without any error How do we jump from one line to other part of code in the same script?

Sudhir said...

Hi Ankur,
In my application there are three drop down each for Date, Month, and Year.Whern we select a particular date and go to the next page the selected date appear as Text element.( mm/dd/yyyy)
I want to first concatenate date from three drop down box and verify it with the next page date which is appearing in mm/dd/yyyy format.

Also if i parameterize the date same shold be work for each iteration.
Can you please help me

steveLanders said...

Reva,

Regular expressions refer to data lexically - so to find if text represents a number between 100 and 632, one could do this:

([1-5][0-9][0-9]) | (6[0-2][0-9]) | (63[0-2])
which means:
100 to 599 OR 600 to 629 OR 630 to 632

What this means:
This breaks up our 3-digit number lexically. One of three cases will be the number:
1. A number between 100 and 599 will include a number from 1 to 5, a number from 0-9, and a number from 0-9

2. If the number starts with a 6, the second digit can only be from 0 to 3. If the second digit is between 0-2, the third digit can be 0-9. The second option accounts for 600 to 629

3. If the first digit is 6, and the second digit is 3, we should limit the range of the third digit to 0-2. The third option accounts for 630 to 632.

I hope that helps.
Steven

Anonymous said...

hi,
i test an application where one of the field gives a text in this format
"ABC already exists."

In my case ABC is a dynamic value and might take any three characters. i tried using the below mentioned formats to identify the object, however it did not seem to work.
*.already exists.
??? already exists.

Please help

Ramakrishna said...

Hi Ankur it's my general doubt not specific to RegExp...if Object spy also could not identify the object properties , means object could not be identified then wat approach we follow to identify tht object? as here Des prog also could not work? is it need to go for obj identification(smart idtn) or anything else approach is there?

Vikass said...

To Ramakrishna,
Did u try Low Level Recording for that object?