Monday, May 7, 2012

VBScript: RegExp

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.

Examples:
 
'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 "
")
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
Syntax: object.Global = [ True | False ]
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.
IgnoreCase
Syntax: object.IgnoreCase
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.
Pattern
Syntax: object.Pattern
This property defines the regular expression or search pattern string that is to be matched during the search.
Methods
Execute
Syntax: object.Execute (TargetString)
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.
Replace
Syntax: object.Replace (String1, String2)
This method is used to replace text found in a regular expression search. Do not confuse this method with the Replace function.
Test
Syntax: object.Test
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.


OBJECT:  Match


The Match object is used to access the three read-only properties associated with the results of a search and match operation that uses a regular expression.

Simply put, a regular expression is a string pattern that you can compare against all or a portion of another string. However, in all fairness, be warned that regular expressions can get very complicated.

The RegExp object can be used to search for and match string patterns in another string. A Match object is created each time the RegExp object finds a match. Since, zero or more matches could be made, the RegExp object actually return a collection of Match objects which is refered to as a Matches collection.

The following code is a simplier, working version of a program published by Microsoft.

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 & "'." & "
" & VBCrLf
        'print
        Response.Write(strReturnStr)
    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:

FirstIndex Property
This property returns the position, counted from the left with the first position being numbered zero, in a string where a match was made.

Syntax: Match.FirstIndex

Length Property
This property returns the length of the matched text found in a search string.

Syntax: Match.Length

Value Property
This property returns the actual text that was matched during the search.

Syntax: Match.Value

No comments: