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

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


8 comments:

  1. hi Nice Post, but can you also tell how to use regular expressions using vbscript in QTP

    ReplyDelete
  2. Thanks for ur "Regular expression" collection

    ReplyDelete
  3. Nice collection, easy to understand even for beginners also.

    ReplyDelete
  4. Hi

    I have doubt in regular expression in Xpath value. i added xpath in Description properties and my value is
    Xapth = *[@id="pg.temp:region_i_id_8:pgtemp:appcount]/table

    in the above value only that particular "8" is differs for each time like it will change to 9 or 2 like that. But in xpath not support for regular expression and contain function use for replace but i m able to replace the contains function. Kindly help me to resolve the problem.

    ReplyDelete
    Replies
    1. Use
      Xapth = *[@id="pg.temp:region_i_id_"& [0-9] &":pgtemp:appcount]/table

      Delete
  5. HI,
    Could you please let me know, how to write RE fro the numbers ex:2,000,50,000 1,00,000 etc,
    Indicating right commas at the right places..

    ReplyDelete
  6. I prefer xpath over <a href="http://webdata-scraping.com>regular expression</a> because of the certain limitations of regular expression in terms of functionality.

    ReplyDelete