Home
Map
Regex.Replace FunctionUse the Regex.Replace Function to perform complex string replacements.
VB.NET
This page was last reviewed on Oct 6, 2023.
Regex.Replace. This VB.NET function performs complex text substitutions. It can use a delegate function for replacements—this is a MatchEvaluator type.
Shows a regex
In simpler cases, it is often sufficient to use a String replacement in VB.NET programs. And for performance, string methods are better.
Let us begin with this program. After declaring the input string, we invoke the Regex.Replace function and pass 3 arguments to it.
Info The arguments to Replace() are input string reference, the pattern to match, and the replacement for matching sequences.
Finally The program demonstrates that the function call performed the task correctly.
Shows a regex
Imports System.Text.RegularExpressions Module Module1 Sub Main() ' Input string. Dim input As String = "abc def axc" ' Use Regex.Replace with string arguments. Dim output As String = Regex.Replace(input, "a..", "CHANGED") ' Print. Console.WriteLine(input) Console.WriteLine(output) End Sub End Module
abc def axc CHANGED def CHANGED
MatchEvaluator. Regex.Replace function has a more powerful version. We must create a MatchEvaluator instance, which is a delegate pointer to a function.
Start Please look at the UpperFirst function. This invokes the Regex.Replace shared method, and passes 3 arguments.
Note The second argument is the pattern of character sequences we want to replace. The third argument is a MatchEvaluator instance.
Tip To create the MatchEvaluator, use the New operator and the AddressOf operator with a function name.
Tip 2 The UpperEvaluator method describes the implementation for the MatchEvaluator. It uppercases only the first character of its input.
AddressOf
Imports System.Text.RegularExpressions Module Module1 Sub Main() ' Write the uppercased forms. Console.WriteLine(UpperFirst("bird")) Console.WriteLine(UpperFirst("bird dog")) Console.WriteLine(UpperFirst("frog")) End Sub Function UpperFirst(ByRef value As String) As String ' Invoke the Regex.Replace function. Return Regex.Replace(value, _ "\b[a-z]\w+", _ New MatchEvaluator(AddressOf UpperEvaluator)) End Function Function UpperEvaluator(ByVal match As Match) As String ' Get string from match. Dim v As String = match.ToString() ' Uppercase only first letter. Return Char.ToUpper(v(0)) + v.Substring(1) End Function End Module
Bird Bird Dog Frog
Notes, performance. We can store the MatchEvaluator instance itself as a field upon the enclosing type. Then, you can simply access this field on each call to Regex.Replace.
And Not only this, but you could cache a Regex object as a field and call Replace on that.
Notes, usage. MatchEvaluator requires an understanding of how to construct an instance by assigning it to the address of an appropriate implementation.
Note You hook up the MatchEvaluator delegate type to an implementation. This must have the correct Match and String types.
Note 2 With MatchEvaluator, we create powerful mechanisms to programmatically mutate matching text patterns.
Summary. Regex.Replace is powerful. Using patterns and special characters, you can add power to your replacements, without adding imperative logic that is hard to maintain.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Oct 6, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.