Microsoft KB Archive/818802

= How To Use Regular Expressions in Microsoft Visual Basic 6.0 =

Article ID: 818802

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-





For a Microsoft Visual Basic .NET version of this article, see 301264.



For a Microsoft Visual C# .NET version of this article, see 308252.

IN THIS TASK

 * SUMMARY
 * Requirements
 * Using Regular Expressions to Match a Pattern
 * Step-by-Step Example
 * REFERENCES



SUMMARY
This step-by-step article describes how to create regular expressions and how to use regular expressions to determine whether strings match specific patterns. Regular expressions allow simple parsing and matching of strings to a specific pattern. If you use the objects that are available in the Microsoft VBScript Regular Expressions 5.5 library, you can compare a string against a specific pattern, replace a string pattern with another string, or retrieve only portions of a formatted string. This article describes how to construct a pattern to parse a string that contains multiple instances of the same pattern.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you require:
 * Microsoft Visual Basic 6.0

This article assumes that you are familiar with the following topics:
 * Visual Basic 6.0
 * Regular expression syntax

back to the top

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.

RegExp provides the following methods to determine whether a string matches a particular pattern of a regular expression:
 * 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:
 * 1) Set the regular expression by using the Pattern method of the RegExp object.
 * 2) Obtain the string to examine with the pattern.
 * 3) Set the IgnoreCase property of the RegExp object to True.
 * 4) Pass the string that you obtained in step 2 as an argument to the Execute method of the RegExp object.
 * 5) Assign the return value of the Execute method to the MatchCollection object.

The MatchCollection object contains information about the matched strings.

Note You can also use the Test method to determine whether the string matches the specific regular expression.

back to the top

Step-by-Step Example
 Start Microsoft Visual Basic 6.0. On the File menu, click New Project. Click Standard Exe in the New Project dialog box, and then click OK.

By default, Form1 is created. On the Project menu, click References. 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.</li> Double-click Command1 to open the Code window.</li>  Paste the following code in the Command1_Click event handler: MsgBox(TestRegExp(&quot;is.&quot;, &quot;IS1 is2 IS3 is4&quot;)) Note In this example, the is. pattern is checked against the &quot;IS1 is2 IS3 is4&quot; 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. </li>  Add the following function after the Command1_click event handler: Function TestRegExp(myPattern As String, myString As String) 'Create objects. 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 & &quot;Match found at position &quot; RetStr = RetStr & objMatch.FirstIndex & &quot;. Match Value is '&quot; RetStr = RetStr & objMatch.Value & &quot;'.&quot; & vbCrLf Next Else RetStr = &quot;String Matching Failed&quot; End If  TestRegExp = RetStr End Function </li> On the Run menu, click Start to run the application.</li> Click Command1.

A message box is displayed that returns all the occurrences of is in the IS1 is2 IS3 is4 string.</li></ol>

back to the top

<div class="references_section">