TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Regex.Replace Function

Regex.Replace performs complex text substitutions. It can use a delegate function for replacements—this is a MatchEvaluator type. But in simpler cases it is often sufficient to use a String replacement.Regex.MatchStrings
Example. To start, please notice the Imports directive. This gives you easier access to all the Regex types. After declaring the input string, we invoke the Regex.Replace function and pass three arguments to it.

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
MatchEvaluator. As mentioned, the Regex.Replace function has a more powerful mechanism for replacing text programmatically. To use this mechanism, you must create a MatchEvaluator instance, which is a delegate pointer to a function.

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.

AddressOf
VB.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
One possible optimization you could employ on this example is to store the MatchEvaluator instance itself as 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.

Using the MatchEvaluator type in VB.NET requires an understanding of how to construct an instance by assigning it to the address of an appropriate implementation. You hook up the MatchEvaluator delegate type to an implementation.

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.

Summary. The Regex.Replace function in VB.NET provides a useful string-based replacement facility. Using patterns and special characters, you can add power to your replacements, without adding imperative logic that is hard to maintain.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf