Introduction
Knowing the Regular Expression-capabilities and
-syntax of Perl, I wanted to have a similar syntax from within my VBScript code.
VBScript uses the
RegExp object, so I wrapped the calls to the RegExp object inside three
function.
Functions
rxTest
function rxTest( text, pattern, flags )
Use this function to test for a pattern inside a string. Acts similar to the
""http:
operator in Perl.
The arguments are:
text
The text string upon which the regular expression is executed.
pattern
Regular string expression being searched for.
flags
i
: case insensitive, g
: global, m
: multiline
Returns true if found, false if not found.
rxReplace
function rxReplace( text, pattern, replace_text, flags )
Use this function to replace a pattern inside a string with another string. Acts similar to the
""http:
operator in Perl.
The arguments are:
text
The text string upon which the regular expression is executed.
pattern
Regular string expression being searched for.
replace_text
The replacement text string.
flags
i
: case insensitive, g
: global, m
: multiline
Returns the modified string. The input-string is not modified by this
function.
rxExec
function rxExec( text, pattern, flags )
Use this function to test for a pattern inside a string and return the
matched elements. Acts similar to the
""http:
operator in Perl.
The arguments are:
text
The text string upon which the regular expression is executed.
pattern
Regular string expression being searched for.
flags
i
: case insensitive, g
: global, m
: multiline
Returns a
""http:
collection if a match is found, otherwise returns nothing
.
Examples
- Replace all ocurrences of a certain syntax by a value, looked up from a
database
do
set matches = rxExec(html, "oid://([0-9]+)", "g")
if not IsNothing(matches) then
if matches.Count>0 then
set match = matches(0)
dim replacement
if match.SubMatches.Count>0 then
replacement = GetNameFromDatabaseByID(match.SubMatches(0))
else
replacement = ""
end if
html = Replace(html, match.Value, replacement)
else
exit do
end if
else
exit do
end if
loop
The last example uses the function IsNothing
which is defined as followed:
function IsNothing( byref obj )
IsNothing = CBool(LCase(TypeName(obj))="nothing")
end function