|
|
Title | Find regular expression matches in a string in VB .NET |
Description | This 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. |
Keywords | regular expression, string, parsing, parse |
Categories | Strings, 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
|
|
|
|
|
|