Regex.Replace function has a more powerful version. We must create a MatchEvaluator instance, which is a delegate pointer to a function.
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