Regex.Replace メソッド

正規表現によって定義されている文字パターンに一致するすべての対象を指定の置換文字列に置き換えます。

オーバーロードの一覧

Regex コンストラクタで指定された正規表現によって定義されている文字列パターンに一致するすべての対象を置き換えます。 MatchEvaluator デリゲートは、一致する対象が見つかるたびに置換を評価するために呼び出されます。

.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;

入力文字列の最初の文字から検索を開始し、指定した正規表現パターンに一致する対象をすべて置換文字列で置き換えます。

.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;

入力文字列の最初の文字から検索を開始し、 Regex コンストラクタで指定した正規表現によって定義されているパターンと一致する対象を指定数だけ置換文字列で置き換えます。 MatchEvaluator デリゲートは、一致する対象が見つかるたびに置換を評価するために呼び出されます。

.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;

入力文字列の最初の文字から検索を開始し、 Regex コンストラクタで指定した正規表現で定義されているパターンに一致する対象を指定数だけ置換文字列で置き換えます。

.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;

最初の文字から検索を開始し、正規表現によって定義されている文字パターンに一致するすべての対象を置換文字列に置き換えます。 MatchEvaluator デリゲートは、一致する対象が見つかるたびに置換を評価するために呼び出されます。

.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;

入力文字列の最初の文字から検索を開始し、正規表現によって定義されている文字パターンに一致するすべての対象を置換文字列で置き換えます。

.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;

入力文字列の指定した文字位置から開始し、 Regex コンストラクタで指定したパターンに一致する対象を指定数だけ置換文字列で置き換えます。 MatchEvaluator デリゲートは、一致する対象が見つかるたびに置換を評価するために呼び出されます。

.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;

入力文字列の指定した文字位置から検索を開始し、 Regex コンストラクタで指定した正規表現によって定義されている入力文字列内のパターンに一致する対象を指定数だけ指定の置換文字列で置き換えます。

.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;

最初の文字から検索を開始し、指定した正規表現によって定義されている文字パターンに一致するすべての対象を置換文字列に置き換えます。オプションを指定して、一致する動作を変更できます。また、一致する対象が見つかるたびに、置換を評価する MatchEvaluator デリゲートが呼び出されます。

.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;

入力文字列内の最初の文字から検索を開始し、指定した正規表現によって定義されているパターンに一致するすべての対象を指定の置換文字列で置き換えます。一致の動作を変更するオプションを指定できます。

.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;

使用例

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

参照

Regex クラス | Regex メンバ | System.Text.RegularExpressions 名前空間