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 Function Replace ( _
	input As String, _
	evaluator As MatchEvaluator _
) As String
Dim instance As Regex
Dim input As String
Dim evaluator As MatchEvaluator
Dim returnValue As String

returnValue = instance.Replace(input, evaluator)
public String Replace (
	String input, 
	MatchEvaluator evaluator
public function Replace (
	input : String, 
	evaluator : MatchEvaluator
) : String



The string to modify.


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.

Imports System.Text.RegularExpressions

Class RegExSample
   Shared Function CapText(m As Match) As String
      ' Get the matched string.
      Dim x As String = m.ToString()
      ' If the first char is lower case...
      If Char.IsLower(x.Chars(0)) Then
         ' Capitalize it.
         Return Char.ToUpper(x.Chars(0)) & x.Substring(1, x.Length - 1)
      End If
      Return x
   End Function    
   Public Shared Sub Main()
      Dim text As String = "four score and seven years ago"
      System.Console.WriteLine("text=[" & text & "]")
      Dim result As String = Regex.Replace(text, "\w+", _
         AddressOf RegExSample.CapText)
      System.Console.WriteLine("result=[" & result & "]")
   End Sub
End Class

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 + "]");

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