Wednesday, July 13, 2011

Regular Expressions in VBScript


You can use regular expressions only for values of type string.
When any special character in a regular expression is preceded by a
backslash (\), QuickTest searches for the literal character.
By default, QuickTest treats all characters in a regular expression literally,
except for the period (.), hyphen (-), asterisk (*), caret (^), brackets ([ ]),
parentheses (()), dollar sign ($), vertical line (|), plus sign (+), question mark
(?), and backslash (\).When one of these special characters is preceded by a
backslash (\), QuickTest treats it as a literal character.
➤ Using the Backslash Character ( \ )
➤ Matching Any Single Character ( . )
➤ Matching Any Single Character in a List ( [xy] )
➤ Matching Any Single Character Not in a List ( [^xy] )
➤ Matching Any Single Character within a Range ( [x-y] )
➤ Matching Zero or More Specific Characters ( * )
➤ Matching One or More Specific Characters ( + )
➤ Matching Zero or One Specific Character ( ? )
➤ Grouping Regular Expressions ( ( ) )
➤ Matching One of Several Regular Expressions ( | )
➤ Matching the Beginning of a Line ( ^ )
➤ Matching the End of a Line ( $ )
➤ Matching Any AlphaNumeric Character Including the Underscore ( \w )
➤ Matching Any Non-AlphaNumeric Character ( \W )
➤ Combining Regular Expression Operators

Using the Backslash Character
A backslash (\) can serve two purposes. It can be used in conjunction with a
special character to indicate that the next character be treated as a literal
character. For example, \. would be treated as period (.) instead of a wildcard.
Alternatively, if the backslash (\) is used in conjunction with some
characters that would otherwise be treated as literal characters, such as the
letters n, t, w, or d, the combination indicates a special character. For
example, \n stands for the newline character.
For example:
➤ w matches the character w
➤ \w is a special character that matches any word character including
underscore
➤ \\ matches the literal character \
➤ \( matches the literal character (
For example, if you were looking for a Web site called:
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
Matching Any Single Character
A period (.) instructs QuickTest to search for any single character (except for
\n). For example:
welcome.
matches welcomes, welcomed, or welcome followed by a space or any other
single character. A series of periods indicates the same number of
unspecified characters.
To match any single character including \n, enter:
(.|\n)
Matching Any Single Character in a List
Square brackets instruct QuickTest to search for any single character within
a list of characters. For example, to search for the date 1967, 1968, or 1969,
enter:
196[789]
Matching Any Single Character Not in a List
When a caret (^) is the first character inside square brackets, it instructs
QuickTest to match any character in the list except for the ones specified in
the string. For example:
[^ab]
matches any character except a or b.
Matching Any Single Character within a Range
In order to match a single character within a range, you can use square
brackets ([ ]) with the hyphen (-) character. For instance, to match any year
in the 1960s, enter:
196[0-9]
A hyphen does not signify a range if it is displayed as the first or last
character within brackets, or after a caret (^).
For example, [-a-z] matches a hyphen or any lowercase letter.
Matching Zero or More Specific Characters
An asterisk (*) instructs QuickTest to match zero or more occurrences of the
preceding character. For example:
ca*r
matches car, caaaaaar, and cr.
Matching One or More Specific Characters
A plus sign (+) instructs QuickTest to match one or more occurrences of the
preceding character. For example:
ca+r
matches car and caaaaaar, but not cr.
Matching Zero or One Specific Character
A question mark (?) instructs QuickTest to match zero or one occurrences of
the preceding character. For example:
ca?r
matches car and cr, but nothing else.
Grouping Regular Expressions
Parentheses (()) instruct QuickTest to treat the contained sequence as a unit,
just as in mathematics and programming languages.
Using groups is especially useful for delimiting the argument(s) to an
alternation operator ( | ) or a repetition operator ( * , + , ? , { } ).
Matching One of Several Regular Expressions
A vertical line (|) instructs QuickTest to match one of a choice of
expressions. For example:
foo|bar
causes QuickTest to match either foo or bar.
fo(o|b)ar
causes QuickTest to match either fooar or fobar.
Matching the Beginning of a Line
A caret (^) instructs QuickTest to match the expression only at the start of a
line, or after a newline character.
For example:
book
matches book within the lines—book, my book, and book list, while
^book
matches book only in the lines—book and book list.
Matching the End of a Line
A dollar sign ($) instructs QuickTest to match the expression only at the end
of a line, or before a newline character. For example:
book
matches book within the lines—my book, and book list, while a string that is
followed by ($), matches only lines ending in that string. For example:
book$
matches book only in the line—my book.
Matching Any AlphaNumeric Character Including the
Underscore
\w instructs QuickTest to match any alphanumeric character and the
underscore (A-Z, a-z, 0-9, _).
For example:
\w* causes QuickTest to match zero or more occurrences of the alphanumeric
characters—A-Z, a-z, 0-9, and the underscore (_). It matches Ab, r9Cj, or
12_uYLgeu_435.
For example:
\w{3} causes QuickTest to match 3 occurrences of the alphanumeric
characters A-Z, a-z, 0-9, and the underscore (_). It matches Ab4, r9_, or z_M.
Matching Any Non-AlphaNumeric Character
\W instructs QuickTest to match any character other than alphanumeric
characters and underscores.
For example:
\W matches &, *, ^, %, $, and # .
Combining Regular Expression Operators
You can combine regular expression operators in a single expression to
achieve the exact search criteria you need.
For example, you can combine the ‘.’ and ‘*’ characters in order to find zero
or more occurrences of any character (except \n).
For example,
start.*
matches start, started, starting, starter, and so forth.
You can use a combination of brackets and an asterisk to limit the search to
a combination of non-numeric characters. For example:
[a-zA-Z]*
To match any number between 0 and 1200, you need to match numbers
with 1 digit, 2 digits, 3 digits, or 4 digits between 1000-1200.
The regular expression below matches any number between 0 and 1200.
([0-9]?[0-9]?[0-9]|1[01][0-9][0-9]|1200)
Example:
Function RegExpTest(patrn, strng)
  Dim regEx, retVal            ' Create variable.
  Set regEx = New RegExp         ' Create regular expression.
  regEx.Pattern = patrn         ' Set pattern.
  regEx.IgnoreCase = False      ' Set case sensitivity.
  retVal = regEx.Test(strng)      ' Execute the search test.
  If retVal Then
    RegExpTest = "One or more matches were found."
  Else
    RegExpTest = "No match was found."
  End If
End Function
MsgBox(RegExpTest("[a-z]", "m"))

1 comment:

Anonymous said...

Nice information provided