The Regular Expression object provides simple regular expression support.
Properties
Global PropertyIgnoreCase Property
Pattern Property
Methods
Execute MethodReplace Method
Test Method
'RegExp
Set regEx = New RegExp ' Create a regular expression.
regEx.Pattern = "[a-zA-Z0-9]" ' Set pattern.
regEx.IgnoreCase = True ' Set case insensitivity.
regEx.Global = True ' Set global applicability.
'RegExp
lovePoem = "How do I love thee? Let me count the ways."
lovePoem = lovePoem & "I love thee to the depth and breadth
and height"
lovePoem = lovePoem & "My soul can reach, when feeling out of
sight"
lovePoem = lovePoem & "For the ends of Being and ideal
Grace."
lovePoem = lovePoem & "I love thee to the level of
everydays"
lovePoem = lovePoem & "Most quiet need, by sun and
candle-light."
lovePoem = lovePoem & "I love thee freely, as men strive for
Right;"
lovePoem = lovePoem & "I love thee purely, as they turn from
Praise."
lovePoem = lovePoem & "I love thee with a passion put to
use"
lovePoem = lovePoem & "In my old griefs, and with my
childhoods faith."
lovePoem = lovePoem & "I love thee with a love I seemed to
lose"
lovePoem = lovePoem & "With my lost saints, — I love thee with
the breath,"
lovePoem = lovePoem & "Smiles, tears, of all my life! — and,
if God choose,"
lovePoem = lovePoem & "I shall but love thee better after
death."
'===============================================================
'Using RegExp — How to count the number of times the word “love”
appears in the poem:
'
'To count the number of times a word (or any pattern) appears in a
string, you can use QTP with VBScript’s RegExp object, along with the Pattern
property and Execute method.
'
'The Pattern property can set or return any regular expression pattern
you wish to search for.
'=============================================================
'The Execute method is used to run a search using a regular expression:
'=============================================================
Set re = New RegExp
re.Global = True
re.Pattern = "love"
Set loveMatch =
re.Execute(lovePoem)
MsgBox loveMatch.Count
'==============================================================
'Replace Method
'
'You can also use the Replace method to count the appearances of a word
or pattern.
'This method replaces the text found in a search using a regular
expression.
'To replace all the occurrences of the word thee with you in the poem
you can do the following:
'===============================================================
Set re = New RegExp
re.Global = True
re.Pattern = "thee"
MsgBox
re.Replace(lovePoem,"you")
Function ReplaceTest(patrn, replStr)
Dim regEx, str1 ' Create variables.
str1 = "The quick brown
fox jumped over the lazy dog."
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True ' Make case insensitive.
ReplaceTest =
regEx.Replace(str1, replStr) ' Make
replacement.
End Function
MsgBox(ReplaceTest("fox", "cat")) ' Replace 'fox' with 'cat'.
'In addition, the Replace method can replace subexpressions in the
pattern. The following call to the function shown in the previous example swaps
each pair of words in the original string:
No comments:
Post a Comment