Home
C#
Regex.Replace Examples: MatchEvaluator
Updated Sep 18, 2024
Dot Net Perls
Regex.Replace. This C# method processes text replacements. It handles simple and complex replacements. For complex patterns, we use a MatchEvaluator delegate to encode the logic.
With Regex.Replace, we can change a string with lowercased words to have uppercased ones. Regex.Replace is powerful—many other replacements can be done.
Regex.Match
This program uses the Regex.Replace static method with a string replacement. It is possible to specify a delegate of type MatchEvaluator for more complex replacements.
Here We use a pattern to replace all 3-letter sequences starting and ending with certain letters with a replacement string.
Tip The Regex method allows you to replace variations in the string in one statement.
using System; using System.Text.RegularExpressions; // Input string. string input = "abc def axe"; Console.WriteLine(input); // Use Regex.Replace to replace the pattern in the input. string output = Regex.Replace(input, @"a..", "CHANGED"); Console.WriteLine(output);
abc def axe CHANGED def CHANGED
MatchEvaluator. We can specify a MatchEvaluator. This is a delegate method that the Regex.Replace method calls to modify the match. Here we use MatchEvaluator to uppercase matches.
delegate
Tip You can use Regex.Replace for simple replacements by using a string argument. For complex replacements, use MatchEvaluator.
Here In Regex.Replace, we use the delegate syntax for a method that alters strings to have an uppercase first letter.
Note Delegate methods are methods you can use as variables and parameters. They introduce some syntactic complexity.
using System; using System.Text.RegularExpressions; class Program { static void Main() { // Input strings. const string s1 = "marcus aurelius"; const string s2 = "the golden bowl"; const string s3 = "Thomas jefferson"; // Write output strings. Console.WriteLine(TextTools.UpperFirst(s1)); Console.WriteLine(TextTools.UpperFirst(s2)); Console.WriteLine(TextTools.UpperFirst(s3)); } } public static class TextTools { /// <summary> /// Uppercase first letters of all words in the string. /// </summary> public static string UpperFirst(string s) { return Regex.Replace(s, @"\b[a-z]\w+", delegate(Match match) { string v = match.ToString(); return char.ToUpper(v[0]) + v.Substring(1); }); } }
Marcus Aurelius The Golden Bowl Thomas Jefferson
Other uses. Microsoft indicates we can use MatchEvaluator to perform validation. We can use it "to perform custom verifications or operations at each Replace operation."
Tip To enhance this capitalization algorithm, you could store a Dictionary of words that need special-casing.
However This requires a bit of manual work to find most of the names using different rules.
Summary. Regex.Replace can be used in 2 ways. The MatchEvaluator delegate offers a high degree of control. With a string, the Regex.Replace method can be used for simpler tasks.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Sep 18, 2024 (edit).
Home
Changes
© 2007-2025 Sam Allen