C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Arguments: The 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.
VB.NET program that uses Regex.Replace
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
' Input string.
Dim input As String = "Dot Net Not Perls"
' Use Regex.Replace with string arguments.
Dim output As String = Regex.Replace(input, "N.t", "NET")
' Print.
Console.WriteLine(input)
Console.WriteLine(output)
End Sub
End Module
Output
Dot Net Not Perls
Dot NET NET Perls
UpperFirst: Please look at the UpperFirst function. This invokes the Regex.Replace shared method, and passes three arguments.
Note: The second argument is the pattern of character sequences we want to replace. The third argument is a MatchEvaluator instance.
New: To create the MatchEvaluator, use the New operator and the AddressOf operator with a function name.
Tip: The UpperEvaluator method describes the implementation for the MatchEvaluator. It uppercases only the first character of its input.
AddressOfVB.NET program that uses Regex.Replace with MatchEvaluator
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
' Three input strings.
Dim a As String = "bluebird bio"
Dim b As String = "intercept pharmaceuticals"
Dim c As String = "puma biotechnology"
' Write the uppercased forms.
Console.WriteLine(UpperFirst(a))
Console.WriteLine(UpperFirst(b))
Console.WriteLine(UpperFirst(c))
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
Output
Bluebird Bio
Intercept Pharmaceuticals
Puma Biotechnology
And: Not only this, but you could cache a Regex object as a field and call Replace on that.
Next: The implementation must receive and return the correct Match and String types.
Review: With MatchEvaluator, we create powerful mechanisms to programmatically mutate matching text patterns.