Export (0) Print
Expand All

Regex.Replace Method (String, MatchEvaluator)

.NET Framework 1.1

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.

[Visual Basic]
Overloads Public Function Replace( _
   ByVal input As String, _
   ByVal evaluator As MatchEvaluator _
) As String
[C#]
public string Replace(
 string input,
 MatchEvaluator evaluator
);
[C++]
public: String* Replace(
 String* input,
 MatchEvaluator* evaluator
);
[JScript]
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.

Remarks

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.

Example

[Visual Basic] 
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

[C#] 
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 + "]");
   }
}

[C++] 
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Text::RegularExpressions;

__gc class RegExSample 
{
   public:
   static String* CapText(Match* m) 
   {
      // Get the matched String.
      String* x = m->ToString();
      // If the first char is lower case...
      if (Char::IsLower(x->Chars[0]))
      {
         // Capitalize it.
         return String::Concat(__box(Char::ToUpper(x->Chars[0])), x->Substring(1, x->Length-1));
      }
      return x;
   }
};

int main() 
{
   String* text = S"four score and seven years ago";
   Console::WriteLine(S"text=[{0}]", text);
   String* result = Regex::Replace(text, S"\\w+", new MatchEvaluator(0, &RegExSample::CapText));
   System::Console::WriteLine(S"result=[{0}]", result);
}

[JScript] 
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();

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Regex Class | Regex Members | System.Text.RegularExpressions Namespace | Regex.Replace Overload List

Show:
© 2015 Microsoft