News:

MyKidsDiary.in :: Capture your kids magical moment and create your Online Private Diary for your kids

Main Menu

Find regular expression matches in a string in VB .NET

Started by nandagopal, Nov 07, 2008, 06:51 PM

Previous topic - Next topic

nandagopal

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