Export (0) Print
Expand All

Regex.Replace Method (String, MatchEvaluator)

Replaces all occurrences of a character pattern defined by the regular expression specified in the Regex constructor. A MatchEvaluator delegate is called at each match to evaluate the replacement.

Namespace: System.Text.RegularExpressions
Assembly: System (in system.dll)

public string Replace (
	string input,
	MatchEvaluator evaluator
)
public String Replace (
	String input, 
	MatchEvaluator evaluator
)
public function Replace (
	input : String, 
	evaluator : MatchEvaluator
) : String

Parameters

input

The string to modify.

evaluator

The MatchEvaluator which evaluates replacement at each step.

Return Value

The modified character string.

The MatchEvaluator type is a delegate that takes a single Match as input and returns a string. It is declared as follows:

public delegate String RegexMatchEvaluator(Match match);

The delegate is called once per match during a replace.

The following code example displays an original string, matches each word in the original string, converts the first character of each match to uppercase, then displays the converted string.

using System.Text.RegularExpressions;

class RegExSample 
{
   static string CapText(Match m) 
   {
      // Get the matched string.
      string x = m.ToString();
      // If the first char is lower case...
      if (char.IsLower(x[0])) 
      {
         // Capitalize it.
         return char.ToUpper(x[0]) + x.Substring(1, x.Length-1);
      }
      return x;
   }
    
   static void Main() 
   {
      string text = "four score and seven years ago";
      System.Console.WriteLine("text=[" + text + "]");
      string result = Regex.Replace(text, @"\w+",
         new MatchEvaluator(RegExSample.CapText));
      System.Console.WriteLine("result=[" + result + "]");
   }
}

import System.Text.RegularExpressions.*;

class RegExSample
{
    static String CapText(Match m)
    {
        // Get the matched string.
        String x = m.ToString();

        // If the first char is lower case...
        if (System.Char.IsLower(x.charAt(0))) {
            // Capitalize it.
            return System.Char.ToUpper(x.charAt(0)) 
                + x.Substring(1, x.get_Length() - 1);
        }
        return x;
    } //CapText

    public static void main(String[] args)
    {
        String text = "four score and seven years ago";
        System.Console.WriteLine("text=[" + text + "]");
        String result = Regex.Replace(text, "\\w+", 
            new MatchEvaluator(RegExSample.CapText));
        System.Console.WriteLine("result=[" + result + "]");
    } //main
} //RegExSample

import System.Text.RegularExpressions;

class RegExSample 
{ 
   static function CapText(m : Match) : String  
   {
      // get the matched string.
      var x : String = m.ToString();
      // If the first char is lower case...
      if (System.Char.IsLower(x[0])) 
      {
         // Capitalize it.
         return System.Char.ToUpper(x[0]) + x.Substring(1, x.Length-1);
      }
      return x;
   }
    
   static function Main() : void 
   {
      var text : String = "four score and seven years ago";
      System.Console.WriteLine("text=[" + text + "]");
      var result : String = Regex.Replace(text, "\\w+", RegExSample.CapText);
      System.Console.WriteLine("result=[" + result + "]");
   }
}
RegExSample.Main();

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft