Export (0) Print
Expand All

Regex.Replace Method

Replaces all occurrences of a character pattern defined by a regular expression with a specified replacement character string.

Overload List

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.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function Replace(String, MatchEvaluator) As String
[C#] public string Replace(string, MatchEvaluator);
[C++] public: String* Replace(String*, MatchEvaluator*);
[JScript] public function Replace(String, MatchEvaluator) : String;

Replaces all occurrences of a specified regular expression pattern with a replacement string, starting at the first character in the input string.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function Replace(String, String) As String
[C#] public string Replace(string, string);
[C++] public: String* Replace(String*, String*);
[JScript] public function Replace(String, String) : String;

Replaces up to a specified number of occurrences of a pattern defined by the regular expression specified in the Regex constructor with a replacement string, starting at the first character in the input string. A MatchEvaluator delegate is called at each match to evaluate the replacement.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function Replace(String, MatchEvaluator, Integer) As String
[C#] public string Replace(string, MatchEvaluator, int);
[C++] public: String* Replace(String*, MatchEvaluator*, int);
[JScript] public function Replace(String, MatchEvaluator, int) : String;

Replaces up to a specified number of occurrences of a pattern defined by the regular expression specified in the Regex constructor with a specified replacement string, starting at the first character in the input string.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function Replace(String, String, Integer) As String
[C#] public string Replace(string, string, int);
[C++] public: String* Replace(String*, String*, int);
[JScript] public function Replace(String, String, int) : String;

Replaces all occurrences of a character pattern defined by a regular expression with a replacement character string starting at the first character. A MatchEvaluator delegate is called at each match to evaluate the replacement.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Replace(String, String, MatchEvaluator) As String
[C#] public static string Replace(string, string, MatchEvaluator);
[C++] public: static String* Replace(String*, String*, MatchEvaluator*);
[JScript] public static function Replace(String, String, MatchEvaluator) : String;

Replaces all occurrences of matches defined by the regular expression with a replacement string, starting at the first character in the input string.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Replace(String, String, String) As String
[C#] public static string Replace(string, string, string);
[C++] public: static String* Replace(String*, String*, String*);
[JScript] public static function Replace(String, String, String) : String;

Replaces up to a specified number of occurrences of a pattern specified in the Regex constructor with a replacement string, starting at a specified character position in the input string. A MatchEvaluator delegate is called at each match to evaluate the replacement.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function Replace(String, MatchEvaluator, Integer, Integer) As String
[C#] public string Replace(string, MatchEvaluator, int, int);
[C++] public: String* Replace(String*, MatchEvaluator*, int, int);
[JScript] public function Replace(String, MatchEvaluator, int, int) : String;

Replaces up to a specified number of occurrences of a pattern in the input string defined by the regular expression specified in the Regex constructor with a specified replacement string, starting at a specified character position in the input string.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function Replace(String, String, Integer, Integer) As String
[C#] public string Replace(string, string, int, int);
[C++] public: String* Replace(String*, String*, int, int);
[JScript] public function Replace(String, String, int, int) : String;

Replaces all occurrences of a character pattern defined by a specified regular expression with a replacement character string starting at the first character. Options can be specified to modify matching behavior and a MatchEvaluator delegate is called at each match to evaluate the replacement.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Replace(String, String, MatchEvaluator, RegexOptions) As String
[C#] public static string Replace(string, string, MatchEvaluator, RegexOptions);
[C++] public: static String* Replace(String*, String*, MatchEvaluator*, RegexOptions);
[JScript] public static function Replace(String, String, MatchEvaluator, RegexOptions) : String;

Replaces all occurrences of a pattern defined by a specified regular expression with a specified replacement character string, starting at the first character in the input string. Options can be specified to modify matching behavior.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Replace(String, String, String, RegexOptions) As String
[C#] public static string Replace(string, string, string, RegexOptions);
[C++] public: static String* Replace(String*, String*, String*, RegexOptions);
[JScript] public static function Replace(String, String, String, RegexOptions) : String;

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();

See Also

Regex Class | Regex Members | System.Text.RegularExpressions Namespace

Show:
© 2014 Microsoft