All Objects
Match Object
Matches Collection
OBJECT: RegExp
Implemented in version 5.0
The
RegExp
object is used to look for and match all occurrences of a search string pattern inside a target string.
Each time the
RegExp
object finds a match during the search, a
Match
object is created and added to a
Matches
collection.
The search pattern is declared using the
Pattern
property. It could, for example, be a simple string, such as "cost analysis". However, the search pattern can also be a regular expression. Regular expressions can range from being very simple to being extremely complex. The
Pattern
property page contains an introductory listing of special characters that can be used with regular expressions.
The following code is a simpler, working version of a program published by Microsoft. Note the use of the keyword
New
when you create a
RegExp
object using
Set
.
Code:
<%
'this sub finds the matches
Sub RegExpTest(strMatchPattern, strPhrase)
'create variables
Dim objRegEx, Match, Matches, StrReturnStr
'create instance of RegExp object
Set objRegEx = New RegExp
'find all matches
objRegEx.Global = True
'set case insensitive
objRegEx.IgnoreCase = True
'set the pattern
objRegEx.Pattern = strMatchPattern
'create the collection of matches
Set Matches = objRegEx.Execute(strPhrase)
'print out all matches
For Each Match in Matches
strReturnStr = "Match found at position "
strReturnStr = strReturnStr & Match.FirstIndex & ". Match Value is '"
strReturnStr = strReturnStr & Match.value & "'."
'print
Response.Write(strReturnStr & "<BR>")
Next
End Sub
'call the subroutine
RegExpTest "is.", "Is1 is2 Is3 is4"
%>
Output:
Match found at position 0. Match Value is 'Is1'.
Match found at position 4. Match Value is 'is2'.
Match found at position 8. Match Value is 'Is3'.
Match found at position 12. Match Value is 'is4'.
PROPERTIES
Global Property
This property is only used with a
RegExp
object variable and returns a
Boolean
value.
False
signifies that a search should only find the first occurrence of a match.
True
signifies that a search should find all occurrences of a match.
Syntax:
object.
Global
= [ True | False ]
IgnoreCase Property
This property is only used with a
RegExp
object variable and returns a
Boolean
value.
False
signifies that a search should be case-sensitive (i.e., upper versus lower case).
True
signifies that a search should ignore case in a match.
Syntax:
object.
IgnoreCase
= [ True | False ]
Pattern Property
This property defines the regular expression or search pattern string that is to be matched during the search.
Syntax:
object.
Pattern
= [ SearchPatternString ]
METHODS
Execute Method
This method is used to execute the search and to look for matches of the search pattern string (or regular expression) and the target string. Each time a match is made, a
Match
object is created and added to a collection that is called a
Matches
collection.
Syntax:
object.
Execute
Replace Method
This method is used to replace text found in a regular expression search. Do not confuse this method with the
Replace
function.
Syntax:
object.
Replace
(String1, String2)
Test Method
This method is used to determine if the search pattern occurs within a specified string and returns a Boolean value to signify the results of the search.
True
is returned if the pattern is found. Otherwise,
False
is returned.
Syntax:
object.
Test
Copyright 1999-2004 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information