|
|
Title | Use regular expressions to replace text in a string in VB .NET |
Description | This example shows how to use regular expressions to replace text in a string in VB .NET. It uses the Regex class's Replace method to make the replacements. |
Keywords | regular expression, string, parsing, parse, replace |
Categories | Strings, Algorithms |
|
|
The program creates a Regex object, passing its constructor a regular expression pattern that will identify text to replace. It calls the object's Replace method, passing it the replacement pattern.
|
|
Private Sub btnGo_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnGo.Click
Dim reg_exp As New Regex(txtPattern.Text)
lblResult.Text = reg_exp.Replace(Me.txtTestString.Text, _
txtReplacementPattern.Text)
End Sub
|
|
In this example, the search pattern is "[aeiouAEIOU]" and the replacement pattern is "." so the program replaces all instances of vowels with periods.
|
|
|
|
|
|