back to the top
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you require:
- Microsoft Visual Basic 6.0
- Visual Basic 6.0
- Regular expression syntax
Using Regular Expressions to Match a Pattern
In Visual Basic 6.0, the RegExp object uses regular expressions to match a pattern. The following properties are provided by RegExp. These properties set the pattern to compare the strings that are passed to the RegExp instance:
- Pattern: A string that defines the regular expression.
- IgnoreCase: A Boolean property that indicates whether you must test the regular expression against all possible matches in a string.
- Global: Sets a Boolean value or returns a Boolean value that indicates whether a pattern must match all the occurrences in a whole search string, or whether a pattern must match just the first occurrence.
- Test: Returns a Boolean value that indicates whether the regular expression can successfully be matched against the string.
- Execute: Returns a MatchCollection object that contains a Match object for each successful match.
To match a string to a regular expression, follow these steps:
- Set the regular expression by using the
Pattern method of the RegExpobject.
- Obtain the string to examine with the pattern.
- Set the IgnoreCase property of the
RegExp object to True.
- Pass the string that you obtained in step 2 as an argument to the Execute method of the RegExpobject.
- Assign the return value of the Executemethod to the MatchCollection object.
MatchCollection object contains information about the matched strings.
back to the top
- Start Microsoft Visual Basic 6.0.
- On the File menu, click
- Click Standard Exe in the New Project dialog box, and then click OK.
By default, Form1 is created.
- On the Project menu, click
- Double-click Microsoft VBScript Regular Expressions 5.5, and then click OK.
- In the toolbox, double-click CommandButton.
By default Command1 is added to the form.
- Double-click Command1 to open the Code window.
- Paste the following code in the
Command1_Click event handler:Note In this example, the is. pattern is checked against the "IS1 is2 IS3 is4" string. You can use the special character period (.) to act as a wildcard character, so that one additional character is matched and displayed with the search pattern. If you add two periods to the search pattern, you see two additional characters. If you do not use any periods, you only see the search pattern.
MsgBox(TestRegExp("is.", "IS1 is2 IS3 is4"))
- Add the following function after the
Command1_click event handler:
Function TestRegExp(myPattern As String, myString As String)
Dim objRegExp As RegExp
Dim objMatch As Match
Dim colMatches As MatchCollection
Dim RetStr As String
' Create a regular expression object.
Set objRegExp = New RegExp
'Set the pattern by using the Pattern property.
objRegExp.Pattern = myPattern
' Set Case Insensitivity.
objRegExp.IgnoreCase = True
'Set global applicability.
objRegExp.Global = True
'Test whether the String can be compared.
If (objRegExp.Test(myString) = True) Then
'Get the matches.
Set colMatches = objRegExp.Execute(myString) ' Execute search.
For Each objMatch In colMatches ' Iterate Matches collection.
RetStr = RetStr & "Match found at position "
RetStr = RetStr & objMatch.FirstIndex & ". Match Value is '"
RetStr = RetStr & objMatch.Value & "'." & vbCrLf
RetStr = "String Matching Failed"
TestRegExp = RetStr
- On the Run menu, click
Start to run the application.
- Click Command1.
A message box is displayed that returns all the occurrences of is in the IS1 is2 IS3 is4 string.
Article ID: 818802 - Last Review: Mar 29, 2017 - Revision: 4