
Regex.Replace processes text replacements. It handles simple string-based replacements and more complex ones. For complex pattern replacements, we use a MatchEvaluator delegate method. We can change a string with lowercased words to have uppercased ones.
This C# article describes Regex.Replace. It provides example programs.

To start, this program is meant to demonstrate the use of the Regex.Replace static method with a string replacement. You can specify a delegate of type MatchEvaluator for more complex replacements. This program simply uses a pattern to replace all three-letter sequences starting and ending with certain letters with a replacement string. The Regex method allows you to replace variations in the string all at once.
Program that uses Regex.Replace method [C#]
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
// This is the input string we are replacing parts from.
string input = "Dot Net Not Perls";
// Use Regex.Replace to replace the pattern in the input.
// ... The pattern N.t indicates three letters, N, any character, and t.
string output = Regex.Replace(input, "N.t", "NET");
// Write the output.
Console.WriteLine(input);
Console.WriteLine(output);
}
}
Output
Dot Net Not Perls
Dot NET NET Perls
Description. This program introduces the Main entry point, and here the input string is assigned. The Regex.Replace method is a public static method and we pass it three string parameters. The first argument is the input; the second argument is the pattern; the third argument is the replacement string data. The program replaces all parts of the source string that are three-character sequences starting with N and ending with lowercase t with another three letters.
Next, we look at an example of using MatchEvaluator. With regular expressions, you can specify a MatchEvaluator. This is a delegate method that the Regex.Replace method will call when you need to modify the match. Here we see how you can use MatchEvaluator to uppercase matches.
Tip: You can use Regex.Replace for simple replacements by using a string argument. For complex replacements, use a MatchEvaluator delegate method.
Program that capitalizes strings [C#]
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
// Input strings.
const string s1 = "samuel allen";
const string s2 = "dot net perls";
const string s3 = "Mother teresa";
// 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);
});
}
}
Output
Samuel Allen
Dot Net Perls
Mother Teresa
Specification
Input: samuel allen
Capitalized: Samuel Allen
Input: dot net perls
Capitalized: Dot Net Perls
Input: Mother teresa
Capitalized: Mother TeresaOverview. The method TextTools.UpperFirst above is called from your code and it uses the regular expression. In Regex.Replace, we use the delegate(Match match) syntax for a private method that alters strings to have an uppercase first letter. Delegate methods are methods you can use as variables and parameters.
Delegate TutorialRegex pattern. It uses escape sequences. The syntax of Regex.Replace's second argument is described here. The metacharacters match very specific patterns of text.
Regex pattern description \b Word break: Matches where a word starts. [a-z] Matches any lowercase ASCII letter. We only need to match words with lowercase first letters. This is a character range expression. \w+ Word characters: Matches must have one or more characters.
Research. When researching the problem, I found a good article at MSDN. However, the solution has some weaknesses: it isn't easy to call elsewhere in your program, and has some extra branches.
MSDN reference page 1
What other uses does MatchEvaluator have? MSDN indicates you can use it when you need to perform validation. "You can use MatchEvaluator to perform custom verifications or operations at each Replace operation."
MSDN reference page 2Enhancement. To enhance this capitalization algorithm, you could store a Dictionary of words that need special-casing, such as DeBruijn. I have used code like that before, and it requires a bit of manual work to find most of the names using different rules.

We looked at examples of the Regex.Replace method, both with a string replacement and with a MatchEvaluator replacement. The MatchEvaluator delegate offers a very fine degree of control, and by basing the uppercase method on it, we can change rules much easier. The Regex.Replace method can used in a simpler way without specifying the MatchEvaluator and instead using a string type.
Regex Type