Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleFind regular expression matches in a string in VB .NET
DescriptionThis example shows how to find regular expression matches in a string in VB .NET. It uses the Regex class's MatchCollection method to get a list of matches.
Keywordsregular expression, string, parsing, parse
CategoriesStrings, Algorithms
 
The program creates a Regex object, passing its constructor a regular expression pattern. It uses the object's MatchCollection method to get a collection of Match objects describing the matches in the string. It copies the input text into a RichTextBox, and then loops through the Match collection making each match's text red in the RichTextBox.
 
Private Sub btnGo_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnGo.Click
    Try
        Dim reg_exp As New Regex(txtPattern.Text)
        Dim matches As MatchCollection
        matches = reg_exp.Matches(txtTestString.Text)

        rchResults.Text = txtTestString.Text
        For Each a_match As Match In matches
            rchResults.Select(a_match.Index, a_match.Length)
            rchResults.SelectionColor = Color.Red
        Next a_match
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated