Monday, January 9, 2012

Regular Expressions

A Regular Expression is a special text string for describing a search for describing a search pattern. You can think of a regular expression as a wild card.You are probably familiar with wild card notations such as *.doc to find all word files in a file manager.

 Regular Expression Characters:

Grouping Regular ExpressionsMultiple Regular Expressions Characters can be combined in a Single Expression. For example :
Window("text:= .*(Report|Graph)").Close
The above line closes a window with Text Report or Graph preceded by any set of alphanumeric characters.
Using  the Backlash Character( \ )
The backlash can be used along with a Regular Expression Character to indicate that the next character be treated as a literal character. ex:-
Browser("Yahoo").Page("Yahoo").Link("text:= .*\..*").Click
Here the backlash is preceded by the .(period) so the .(period) will be considered literal & not a Regular Expression.

Regexp Object
You can use regular expressions in VBScript by creating one or more instances of the RegExp object. This object allows you to find regular expression matches in strings, and replace regexp matches in strings with other strings. You can create this object in scripting as shown below:
Regexp Rx = New Regexp


  1. Matching Alphanumeric String
    Description: Matches any alphanumeric string without any spaces.
    Matching Text : 9c, XYZ , B6H8
    Non Matching Text: 14.5
  2. Matching an Email Address
    Description: Matches any email ids in the format
    (\w+) - Matches any Firstname with any alphanumeric characters & Underscore
    \. - Treat dot as a literal character. Slash(\) is an escape character which escapes the special meaning
         of dot(.)
    (w+) - Matches any lastname with any Alphanumeric character & Underscore
    @ - Email id henceforth should be followed by @
    (\w+) - Matches any company name with any Alphanumeric characters & underscore
    \. - Company name followed by a dot
    [A-Za-z] {2,4}  - Matches alphabets of both lower case and uppercase. Number of characters can range from two to four.

    Matching Text :
    Non Matching Text: dce3062@gmail.xyzab
  3. Matching Credit Card
    Description: The above expression validates against a visa card number. Here all visa card numbers should start with a digit 4 and are followed by numbers ranging from 12 to 15 numbers.
    Matching Text: 418563256985214 , 4125632569856321
    Non Matching Text:
    3125652365214 : Here the first digit is not 4 hence the string is not matched

No comments:

Post a Comment